How To Exclude Category From WordPress HomePage?

If you want to removed the posts from certain category to not be displayed in your WordPress homepage then, you have come to the right place. By default, WordPress will show every posts of each category in blog page. However, here, we will learn about how to exclude category from WordPress homepage of your site.

Why you may want to exclude certain category from the blog/home page of your site?

In some cases, you may have categorized certain category in your site to display certain portion/details within your site. So, that may not be the blog post listing and hence, it is probably the good idea to exclude that category from the home page.

And as we stated earlier, WordPress will not filter out any of the category added in your site by default. It will show every posts created in your site with every category.

So now, let’s learn ahead about how to exclude category from WordPress homepage of your site, shall we?

How to exclude category from WordPress homepage?

WordPress, has given you an excellent filter to do so for within your site. And the filter hook is: pre_get_posts. So, here we will learn on this filter about how to exclude category from WordPress homepage. Hence let’s move ahead and learn about how to do so, shall we?

As from the previous link, we can know that: pre_get_posts action gives developers access to the $query object by reference. So, we can assume that using it within the theme’s functions.php file, we can achieve this very goal, which we are seeking for.

Hence to exclude category from WordPress homepage, add the below PHP code in your functions.php file of the theme:

function theme_slug_exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-1' ); // Change -1 to the required category id of your site.
}

return $query;
}
add_filter( 'pre_get_posts', 'theme_slug_exclude_category_home' );

Now, in the above code, you need to set the value of -1 as needed for your site. Here, in our case, it is Uncategorized category. In your case, it may vary. So, find the correct id of that category within your site and then, replace the above code with the correct ones as needed.

And if you want to exclude 2 or more than two categories then, you can modify the above code as below:

function theme_slug_exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-1, -10, -20' );
}

return $query;
}
add_filter( 'pre_get_posts', 'theme_slug_exclude_category_home' );

Conclusion

Now, if you have followed this tutorial properly and can now, easily exclude category from WordPress homepage then, we can assume that you are more familiar on this than before. However, if you are still confused and want to know more then, do drop the comment below on the comment box and we will follow it up.

You may also want to read:

  1. How To Get Image Details With Image URL In WordPress?
  2. How To Open WordPress Menu Items In New Tab/Window
  3. Selective Refresh In Customizer Options
  4. Make Your Theme WooCommerce Plugin Compatible
  5. Tutorial On Creating WordPress Child Theme
  6. How To Filter WordPress Content

Post navigation

Bishal Napit

Bishal Napit is a WordPress theme developer from Tansen, Palpa, with a passion to learn more on WordPress.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.