WordPress: Exclude articles from a category on the homepage

Sometimes you don't want to display certain articles in the last blog posts. In this article I show you how you can choose to exclude the contributions of one or more categories on the start page with either a plugin or code snippet.

The reasons for this can be varied. Perhaps the latest customer projects should be presented in a portfolio category, which can only be accessed from the menu, but whose contributions should not appear in the news. The same is conceivable for testimonials or other types of content.

Especially with simple pages, you don't always need a full-fledged plug-in with additional custom post types, but you are satisfied with the standard features of the core.

Hide the posts of a category in the blog with the WordPress plugin

To hide posts using a plugin, I recommend Ultimate Category Excluder , which worked flawlessly in my tests and as described. It can be downloaded for free from the WordPress plugin directory and is active on over 30,000 websites.

Ultimate Category Excluder allows you to quickly and easily exclude categories from the start page, news feed, archive page or search results.

From: Marios Alexandrou

( 64 )
Last updated: 2 weeks ago
50,000+ active installations
Compatible up to: 5.6

After activating the plug-in, the categories to be excluded can be configured under Settings → Category Excluder .

In addition to the start page or blog index, the posts in a category can also be excluded from RSS feeds and from the search pages. With the option “Exclude from All Archives” the archive page of the whole category is hidden, ie here in the example screenshot the URL / category / slider / leads to a 404 error page.

Remove categories in the blog with code snippets

If you don't want to use an extra plugin, you can exclude certain categories from the WordPress loop on the start page with a small snippet of code.

To change the main query to exclude the categories, we use the pre_get_posts filter hook. The if query ensures that the posts are only hidden on the start page and in the standard loop.

 function theme_slug_exclude_categories( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-7,-132' ); } } add_action( 'pre_get_posts', 'theme_slug_exclude_categories' );

With the code snippet, all contributions in the categories with the IDs 7 and 132 are excluded. The minus in front of the IDs ensures that the categories are excluded.

The ID of the categories can be determined by editing them under Articles → Categories . The ID can then be read in the browser URL:
/wp-admin/term.php?taxonomy=category&tag_ID= 7

The parameter cat can also be used to display only the contributions of this category (include) by specifying the ID without minus. If the blog should only show the category News and not the 85 other categories, this code can be used:

 function theme_slug_include_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '13' ); } } add_action( 'pre_get_posts', 'theme_slug_include_category' );

The code can be adapted as required, for example to hide the categories on other pages. This snippet removes all posts in the category with ID 5 from both the search and the blog index.

 function theme_slug_exclude_categories( $query ) { if ( ! is_admin() && $query->is_main_query() ) { if ( $query->is_home() or $query->is_search() ) { $query->set( 'cat', '-5' ); } } } add_action( 'pre_get_posts', 'theme_slug_exclude_categories' );

For the use of the code snippets, I recommend creating a WordPress child theme .

Source of the snippets: WordPress Codex