How to Exclude a Category From Your WordPress Homepage

How to Exclude a Category From Your WordPress Homepage

Do you want to exclude a specific category from your website’s homepage? By default, WordPress displays posts from all categories on your own homepage. In some cases, you might not want that. In this blog post, we are going to show you how you can very easily exclude a category from your WordPress homepage.

Why Exclude a Category from WordPress Homepage?

WordPress enables you to sort content into categories and tags. Now sometimes you might want to use a category for posts that are not part of your normal blog entries.

By default, WordPress will not let you to hide posts in specific categories from homepage or archives. This leaves you with unwanted posts appearing on your homepage.

That being said, let us see how you will be able to easily exclude a specific category from your WordPress homepage. we are going to show you two different methods, and you will be able to select the the one which best fits your needs.

Method 1: Exclude a Category from WordPress Using Plugin

First thing you have to do is to install and activate the Ultimate Category Excluder plugin. For more details, you should follow our guide about how to install a WordPress plugin.

Upon activation, you will have to go to Settings » Category Excluder page. It will show all the categories that are available on your WordPress website.

From here, you will be able to simply check one or multiple categories under the ‘Exclude from front page’ column. In case you want, then you will be able to also exclude the category from RSS feeds, archives, or even from WordPress search.

Do not forget to click on the Update button to store these settings.

Now you will be able to visit your website to view that the posts from the selected categories are excluded from the front page.

Method 2: Exclude a Category from WordPress Homepage Using Code

This method requires you to add code to your WordPress files. In case you have not done this before, then see our guide about how to copy and paste code snippets in WordPress.

You have to add following code to your theme’s functions.php file or perhaps a site-specific plugin.

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

Do not forget to replace ID (-3) with your category ID. It will hide all blog posts from homepage belonging to the category that matches this ID.

Note: Make sure to add a minus (-) sign with the category ID.

Not sure how you can find the category ID? See our guide about how to find category IDs in WordPress.

In case you wish to exclude multiple categories, then you to change the code like this:

function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( ‘cat’, ‘-3, -9, -21’ );
}
return $query;
}
add_filter( ‘pre_get_posts’, ‘exclude_category_home’ );

Simply replace the IDs (-3, -9, -21) with your category IDs to exclude them from your WordPress homepage.

We hope this blog post helped you learn how to exclude a category from your WordPress homepage. You might also want to see these most wanted category hacks and plugins for WordPress that will help you use categories much more efficiently.

In case you liked this blog post, then please sign up to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Also published on Medium.