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.
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
Hello Brian,
Your entry seems to be about exactly what I'm looking for.
Where does the code have to be inserted to make it work?
I would like to filter posts on the home page as well as on another page.
I look forward to a fixed response.
LG
Astrid
Hello Astrid,
The functions can, for example, be added to a child theme in functions.php. For the creation of a child theme see also https://themekiller.me/2016/09/03/ein-wordpress-child-theme-erstellen-stufe-fuer-stufe-anleitung/
Otherwise code snippets can also be inserted via plugin: https://themekiller.me/2017/02/02/wordpress-code-schnipsel-mit-plugin-hinzufuegen-und-verwalten/
In this case, however, it is probably more advisable to use the Ultimate Category Excluder Plugin instead of your own code snippets.
LG,
Brian
Hey brian,
thank you for your quick feedback. Unfortunately it doesn't work for me (bento-theme).
If I delete "$ query-> is_main_query ()" it works, but then my page navigation is gone.
Could it be that I have to use a different term instead? If so, where can I find it?
Unfortunately, I'm not that familiar with programming yet, but the filtering of the categories must also take place on another page. 🙁
If it works without is_main_query (), then it is probably a custom query in the theme.
The only thing that helps here is to contact the theme author. This can provide information on whether and how the database question of the theme can be adapted.
LG,
Brian
Thanks for the tip: I installed the plugin and everything went by itself 🙂
Happy, happy freut