Quite often, clients ask me to exclude one or more categories from their site home page. Surprisingly, there’s no chance to set this from the WordPress admin section. That’s why, some time ago, I wrote the following function:
<?php function nice_exclude_cat( $query ) { if ( $query->is_home() && $query->is_main_query() ) $query->set( 'cat', '-1,-2,-3' ); return $query; } add_filter( 'pre_get_posts', 'nice_exclude_cat' ); ?>
Copy and paste the code above into your theme´s functions.php file and voilá.
For this filter, you can set as many categories as you want (replace ‘-1,-2,-3’ with the IDs of the categories you need to exclude). Please remember to separate them with commas and set the negative sign before the category ID.
This is a great example of hacking WordPress core functionalities using filters. (Note to myself: I need to write about this)
I hope you find it useful.
Leave a Reply