If you looking for a way to exclude categories from your home page then here is a simple tutorial that will help you learn how to exclude one or more categories from your WordPress home page or archive page.
Method 1: Using PHP code
Add the below code to your theme’s functions.php file. Be careful, incorrectly editing your theme’s code can crash your site.
add_action( 'pre_get_posts', 'xt_query_exclude_category' ); function xt_query_exclude_category( $query ) { if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) ) $query->set( 'cat', '-5' ); }
The above code excludes category with ID 5 from all loops accept when viewing the website logged with an administrator ID. The trick is done with the line $query->set( 'cat', '-5' )
Note that there is a negative sign before the ID which tells WordPress to exclude that category. You can exclude multiple categories by adding all the category IDs separated by comma. e.g. $query->set( 'cat', '-5,-3,-10' )
Method 2: Using Plugin
WordPress plugin directory has 45,000+ plugins and guess what if you are looking for a WordPress Mod then go and search for a WordPress plugin. There is high probable chance that someone might have already made a plugin for it. If you are someone will little knowledge of programming then I would prefer that you use Ultimate Category Excluder plugin to exclude categories from displaying on home page or archive pages.
Some of the noted features of the Ultimate Category Excluder plugin–
- Easy Checkbox style option page to select which categories to exclude.
- Ability to exclude different category for different page, namely different settings for front page/home page, archive page, feeds and search result page.
Do you like this article?