How to Remove Posts from the Homepage in WordPress

By default, new WordPress posts appear at the front of your blog. However, sometimes you don’t want this to happen! Here’s how to remove posts from the home page, so they don’t appear in your feed.

Step 1: Create a New “Dummy” Category

The best way to keep some posts away from the front of your blog is to create a new category for them. I created one called “NF” – standing for “Not Front”.

Create a New WordPress Category
Create a New WordPress Category

Step 2: Get the Category ID Number

Every category (and tag) in WordPress is represented internally by an ID number. We need to find this number for our newly created category. To do this, navigate to Posts -> Categories from your WordPress dashboard. You newly created category should be in the list on the right hand side. Hover your mouse pointer over the name, or over the “Edit” link below it.

Now look at the status bar for the link. You should see something like “tag_ID=XXX where “XXX” is the category ID:

Remove Posts from the Homepage in WordPress

In my example, the category ID is 15.

Step 3: Insert Code in functions.php

This last step requires you to add code into your WordPress functions.php or custom PHP plugin. If you don’t know how to do this, read my tutorial on adding snippets to your WordPress installation. Here is the code you need to use:

function exclude_pages_from_front($query) {
      if ( $query->is_home() && $query->is_main_query() ) {
      $query->set( 'cat', '-15' );
    }
}
add_action('pre_get_posts','exclude_pages_from_front');

In line 3, replace the section in bold with the category number you obtained in Step 2. Don’t forget to include the minus (-) sign before it! This will exclude all posts with the specified category number from the WordPress homepage.

And so the next time you want to prevent a post from appearing on your blog’s front page, simply select the new category you created in Step 1, and voila! It’s hidden from the front.

(Optional) Filtering Custom Post Types Instead

The above code filters the query based on a category ID. However, custom post types are also very useful, and this filter will not work with them. To filter based on custom post type instead of categories, replace:

$query->set( 'cat', '-15' );

with

$custom_query = array(
'taxonomy' => 'portfolio',
'operator' => 'NOT IN',
);
$query->set( 'tax_query', $custom_query );

The above code will ensure that the “portfolio” custom post doesn’t show up in the search results. Nifty eh?

Using a Plugin

Not everyone’s comfortable with inserting code into their WordPress site. In which case, I suggest you use the “Ultimate Category Excluder” or “UCE” plugin. This allows you to choose any given category and exclude it from a bunch of places like the homepage, feeds, archives, and search. Great a for a quick fix and useful in the long run too!

About Bhagwad Park

I've been writing about web hosting and WordPress tutorials since 2008. I also create tutorials on Linux server administration, and have a ton of experience with web hosting products. Contact me via e-mail!

Speak Your Mind

*

WP-Tweaks