How to exclude categories from the home page of your WordPress Blog

Sometimes you may need to hide certain categories from your WordPress Blog. Currently there is no in-built facility available in WordPress to hide categories though there are many themes available to with this functionality. If your WordPress theme does not have this option, you will require making following changes to hide categories from your blog home page.

[1] Obtain category ID:

In the recent WordPress version (> 3.0),  you cannot obtain the category ID from your WordPress admin but you can use the plugin like Reveal IDs to obtain hidden IDs.

[2]Add required code in theme’s functions.php:

Now, go to Appearance > Editor and open functions.php. Add the following function at the bottom:

function exclude_category($query) {
if ( $query->is_home ) {
$query->set(‘cat’, ‘-xx’);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘exclude_category’);

The above code must be written within php tag.

If you want to block multiple categories, use the following condition:

$query->set(‘cat’, ‘-1 -2 -3 -4 -5’);

Posted in WordPress KB.

Leave a Reply

Your email address will not be published. Required fields are marked *