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 help companies make the most of content. Some people call me a writer, others a content manager. I prefer "someone who gets results".

Need a hand? Get in touch.

  • twitter
  • pinterest
  • linkedin

Stay in touch

Sign up for weekly learnings from my work and projects, commentary on content and links I've found useful on my travels.

Your email address will not be shared.

Using stock photography to lift your content marketing

Starting a business? Thoughts and reflections on where to begin

What is Search Engine Marketing? An introduction for small business

How to use employees to make your marketing authentic

Fixing “Page Title Not Defined” in Matomo analytics

UK’s Plastic Waste Problem : turning web content to a new use

How I create charts for reports without using Excel

Photographs from December: Reliants, Thunderbirds and shorter nights