Excluding Posts from Your Wordpress Feed

by Scott Jangro on 01 March 2008

rabbit rabbit“. Happy March.

While it’s not my primary blogging subject, I’ve got a major subculture here around the subject of DIY electronics repair. My Samsung DLP television is my usual guinea pig, and I get a decent amount of traffic on that topic.

Naturally, I want to write more about that and I have plans to do so, but annoying my regular readers, the people I know personally, with that stuff has always kept me in check.

I discovered this morning that I can write about that stuff and keep those posts out of my regular feed as well as keep them off the homepage if I wish.

It’s quite simple to do so. Just add the following code to your template’s functions.php file:

function myFilter($query) {
	if ($query->is_feed) {
		$query->set('cat','-5');
	}

return $query;
}

add_filter('pre_get_posts','myFilter');

That will keep category with ID 5 out of the feed. You can get the IDs by looking in the wp_terms table in your database. With wordpress 2.1, these terms are tags and categories, and while I haven’t tested it with tags, I have seen that it does work with Categories.

If you want to exclude more than one category, put them in separated by commas ‘-5,-10,-11′

This is a great solution if you’ve got some test posts or otherwise stuff you want to post but not hit the RSS. For example, if you’re pulling all your twitter posts into your blog, you can avoid cluttering up your RSS with this info.

Also, if you want to keep posts off the homepage, you use if ($query->is_home) instead of is_feed. Or you can do both with this: if ($query->is_home || $query->is_feed)

Thanks to Zeo for this great info.

  • Thanks for this.
  • icenslice
    Sometimes I update a blog post and I don't want it to go through the feed again (and it does...using Feedburner).
    How can I filter for that?

    Thanks for the great WP tips!
  • Nice tutorial, thank you.
  • BS
    That works to remove undesired categories from the main feed, but it prevents you from generating individual feeds for individual categories.

    For example, our main site feed is at:

    http://www.mysite.com/index.php/feed

    We also had individual category feeds available on each category page using somthing like the following:

    http://www.mysite.com/?cat=8&feed=rss2

    Those individual category feeds now all display the main site feed. I'm guessing that's because the code you provided sets the categories to display. Is there any way to alter the code so that it only applies to the main site feed?
  • Thanks for this. It is what I'm looking for!
  • Nico
    Has anyone seemed to figure out how to fix the problems of reversed order when using this function? It seems that when I include this in functions.php all my posts are shown in reverse chronological order.
  • Nice trick, thanks for that, I'll be using that in my sites to lop off spurious content from the feeds...
  • Jessica
    I didn't want to exclude a whole category from my feed, so I made a tag to add to entries I didn't want in the feed instead. I changed "cat" in the code you gave to "tag" (and the ID, obviously), and it works perfectly - thank you for that!
  • @Vlad glad you like my name ;) You've probably right about the 404 page... I'll try this on a test blog.
  • Can't you set up seperate feeds for seperate categories? There may not be a plugin to do it easily, but it should be doable.
blog comments powered by Disqus