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.

Posted on Saturday, March 1, 2008 at 10:30:18 AM in Wordpress
blog comments powered by Disqus