
Do you need to exclude a particular category of posts from display on the WordPress loop? If yes, here is a handy code snippet to do so.

Do you need to exclude a particular category of posts from display on the WordPress loop? If yes, here is a handy code snippet to do so.
Paste the code below into your functions.php file. Don't forget to update the code with the category ID you want to exclude (on line 4).
// No "Quick Tips" on the homepage
function preventHomepageTips($query) {
if($query->is_home() && $query->is_main_query()) {
$query->set('cat', '-40'); // 40 is Quick Tips's category ID
}
}
add_action('pre_get_posts', 'preventHomepageTips');
Note that David Walsh, the author of this code, have created a plugin which do the same thing as this snippet.
Thanks to David Walsh for the code!
One Response
or you can use
$query->set(‘cat’, ‘-’.get_cat_ID(‘CategoryName’));
to avoid hardcoding IDs.
Trackbacks: