comments Written By: Scott Jangro
March 1, 2008

Excluding Posts from Your Wordpress Feed

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.

If you enjoyed this post, please subscribe to my RSS feed

15 Comments... What do you think?


  1. Vlad said on March 1st, 2008 at 2:14 pm

    This is good. I was looking for something like this for quiet some time. Thanks.

  2. Vlad said on March 1st, 2008 at 2:22 pm

    What about the readers that would be interested just in those posts? Just relying on SE to index those pages?

  3. Scott said on March 1st, 2008 at 2:30 pm

    Good question Vlad. I’m not sure, but I think you can get feeds on specific categories from wordpress. I’d have to research that a bit.

  4. Vlad said on March 1st, 2008 at 2:43 pm

    Please update here if you find a solution. I am working on something similar.

  5. brackets said on March 1st, 2008 at 10:26 pm

    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.

  6. design hotels said on March 2nd, 2008 at 4:31 pm

    for category feed you must use this : http://example.com/category/categoryname/feed more information here : http://codex.wordpress.org/WordPress_Feeds

  7. sachinKT said on March 5th, 2008 at 1:27 pm

    thanks for the info !! i will try it ….

  8. Vlad said on March 5th, 2008 at 1:34 pm

    @desing hotels-??? (cute name ;) )

    Your suggestion will only work if you decide exclude posts on the home page of the blog. The code that Scot is going to use is placed in functions.php - I am afraid that it will either give an error or 404 page for the path “/categoryname/feed”

  9. design hotels said on March 5th, 2008 at 2:26 pm

    @Vlad glad you like my name ;) You’ve probably right about the 404 page… I’ll try this on a test blog.

  10. Vlad said on March 5th, 2008 at 2:34 pm

    @design hotels

    I am interested in this solution. Currently I have a category on one of my wordpress sites where I exclude certain category from the front page but not from the main feed. I did not have time to try if modifying functions.php will affect the “category feed”

  11. Jessica said on April 17th, 2008 at 1:00 am

    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!

  12. nonhocapito said on April 29th, 2008 at 7:36 am

    when I try this trick under WP 2.5 with feeds I get all the post in REVERSED order! I think it is because of the new RANDOM order introduced with 2.5. So far I was not able to come with any solution… to set query['order'] as well doesn’t work.

  13. bay of islands nz said on May 20th, 2008 at 11:03 am

    Nice trick, thanks for that, I’ll be using that in my sites to lop off spurious content from the feeds…

  14. Exclure certains articles de votre flux RSS said on March 4th, 2008 at 1:46 am

    [...] Via Jangro [...]

  15. [...] Excluding Posts from Your Wordpress Feed 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. [...]

Join the discussion by leaving a comment...

How do I change my avatar?

Go to gravatar.com and upload your preferred avatar.