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

I'm a freelance content manager and editorial designer. I work with small and growing businesses so they get the most out of their content.

See what I do and get in touch.

  • twitter
  • pinterest
  • linkedin

How to use the skyscraper technique to boost your content marketing

What happened to the navigation?

The photographs of October: churches, houses and a castle

Document layouts: examples and concepts

Macbeth and a coincidence of design

The way Behance displays artwork sucks

How to reverse engineer a spreadsheet

If you want to be an influencer remember you’re in business