Exclude a Category From Your Homepage in WordPress

Last updated on June 26th, 2022 at 11:41 am

There are many reasons for you to exclude a category from your WordPress homepage. The main reason could be that by default WordPress shows all categories on your site, and in many cases having all of them listed especially on your home page is not a good idea. Follow these steps to exclude a category from your homepage in WordPress.

 

Why Would You Want to Exclude a Category From Your Homepage in WordPress?

If your regular blog post structure includes categories for posts that are not usually part of your blog posts, then you may need to hide the categories for such posts so that they do not appear on the site.

WordPress does not have an option to hide a specific category by default. You can easily achieve this by using one of the two methods for hiding a category in WordPress.

Method 1: Automatically Exclude a Category in WordPress With a Plugin

 

In order to do this, you need to install a plugin such as the Simple Exclude Categories plugin. Once you activate the plugin, go to Settings -> Reading and choose the category that you want to hide. Click on Save Changes.

 

Another plugin for excluding the categories from your homepage is the Ultimate Category Excluder plugin. It has some additional options such as the one for excluding a category from your WordPress feed, etc.

Once you install and activate the Ultimate Category Excluder plugin, go to Settings -> Category Exclusion.

 

Check the categories that you want to exclude from your Main Page and click on the Update button to save the changes.

With this plugin you can also hide your categories from Archives, Feeds and Searches.

Now check your site to see if the needed categories are excluded from your homepage.

 

Method 2: Manually Exclude a Category in WordPress With a Code Snippet

If you are not into installing any additional plugins on your site, then use this code snippet to exclude a category in WordPress:

 

function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );

 

As you may already know, you need to copy and paste this code snippet to your functions.php file. If you are not familiar with adding new code to your functions.php, we recommend the My Custom Functions that will help you easily and safely add your custom PHP functions to your site.
Be sure to change ‘-3’ to the category ID that you want to hide. This will hide all of the posts that are under this category from your homepage.
In order to exclude multiple categories from your homepage, do the following changes to your code:

 

function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-3, -5, -12' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );

 

This means that you need to replace '-3, -5, -12' with your own category IDs.
I hope this tutorial helped you to exclude a category from your homepage in WordPress. Please let me know in the comments below.

 

Leave a Reply

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