Exclude a Category from WordPress Homepage Using Code

Exclude a Category from WordPress Homepage Using Code

This method requires you to add code to your WordPress files. If you haven’t done this before, then see our guide on how to copy and paste code snippets in WordPress.

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 our 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. You may also want to see these most wanted category hacks and plugins for WordPress that will help you use categories more efficiently.

see source

Leave a Reply

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

Do NOT follow this link or you will be banned from the site!
%d bloggers like this: