Excluding categories from your blog in WordPress

Excluding categories from your blog in WordPress

If you want to suppress a particular category from your WordPress blog it’s quite easy to do. All you need is the following snippet of code:

function exclude_category_home( $query ) {
// I exclude a specific category from the home page.
if ( $query->is_home ) {
$query->set( ‘cat’, ‘-139’ );
}
return $query;
}

add_filter( ‘pre_get_posts’, ‘exclude_category_home’ );

The function tells the index query to omit category 139. On my blog, 139 is the “notebook” category where I keep my random thoughts and notes that don’t fit well in a blog structure. I suggest you use the ID as its more reliable than trying to infer it from the category name.

To find your category’s ID, edit the category in WordPress and look at the URL:

/wp-admin/term.php?taxonomy=category&tag_ID=139&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

The bit in bold (tag_ID=139) gives you the ID.

“add_filter” tells WordPress to call the function before the loop.

I’ve put this code in my site’s plugin so it doesn’t get lost the next time I change the theme.

It’s a useful piece of code if you do article spinning or want to keep certain things off your blog.

About Ross A Hall

A business researcher and writer, I help companies find new markets, form strategies and build successful businesses.

Find out more about my work.

  • linkedin