How to Exclude a Category From Your WordPress Homepage

Do you need to exclude a particular class from your website’s homepage? By default, WordPress displays posts from all classes on your homepage. In some cases, you’ll not need that. in this article, we’ll show you the way to simply exclude a class from your WordPress homepage.

Why Exclude a Category from WordPress Homepage?

WordPress permits you to type content into classes and tags. currently, typically you will need to use a class for posts that don’t seem to be a part of your traditional weblog entries

Of course, WordPress does not enable you to conceal posts in particular classifications from landing page or files. This abandons you with undesirable posts showing up on your landing page.

Of course, WordPress does not enable you to conceal posts in particular classifications from landing page or files. This abandons you with undesirable posts showing up on your landing page.

Method 1: Exclude a Category from WordPress Using Plugin

The first thing you need to do is to install and activate the Ultimate Category Excluder plugin. For more details,

Upon activation, you’ll need to go to Settings » Category Excluder page. It will display all the categories that are available on your WordPress blog.

From here, you can simply check one or multiple categories under the ‘Exclude from front page’ column. If you want, then you can also exclude the category from RSS feeds, archives, or even from WordPress search.

Don’t forget to click on the Update button to store these settings.

Now you can visit your website to see that the posts from the selected categories are excluded from the front page.

Method 2: Exclude a Category from WordPress Homepage Using Code

This techniqueneeds you to feature code to your WordPress files. If you haven’t done this before

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

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

Don’t forget to replace ID (-5) 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 to find the category ID? See the guide on how to find category IDs in WordPress.

If you want to exclude multiple categories, then you can change the code like this:

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

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

We hope this article helped you learn how to exclude a category from your WordPress homepage

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here