How to: exclude categories from your rss feed

Depending to your blog structure, it may be interesting to exclude some categories from your rss feeds. If you always wanted to find a clean way to do it, I’m pretty sure this recipe will make you happy.

Before starting to code, you'll have to know the numeric ID of the categories you want to exclude. If you don't know how to get the ID of a particular category, just read this recipe.

Once you have the ID of the categories you want to exclude from your rss feed, open the functions.php file from your theme. If your theme doesn't have a functions.php file, create one.

Paste the following code in it:

function myFilter($query) {
    if ($query->is_feed) {
        $query->set('cat','-5'); //Don't forget to change the category ID =^o^=
    }
return $query;
}

add_filter('pre_get_posts','myFilter');

Credit goes to Scott Jangro for this awesome recipe!

30 Responses

Oct 25 2008 00:00

That’s pretty cool, especially if you have a section where you write things unrelated to your blog which might put your regular readers off :)

Oct 25 2008 02:10

I love this idea the more I think about it. Thank you so much!

Oct 25 2008 07:13

You’re welcome, guys! Glad you enjoyed this recipe.

Oct 28 2008 08:46

I like this idea. never heard of this before

Oct 31 2008 23:27

Thanks. This tip is small but useful :) .

Oct 31 2008 23:35

Good idea, great note :) Thank you…

Nov 01 2008 02:34

this is it. been wondering how to do that. thanks!

Dec 03 2008 12:26

good one, but is there a way to have a RSS feed of a given category and all it’s children.. shall I remove the minus, and have alll the categories like
$query->set(‘cat’,'5,6,7′);
or is there a way to have the children categories listed automatically?

thank you in advance

Dec 04 2008 04:51

@Fromtheold, agreed i post some posts on my blog which are sometimes off topic and put off the readers. Excluding those categories would help.

Thanks for the tip.

Mar 19 2009 16:08

Very good! But it seems not work Wordpress2.7. I do not understand:-(

Jan 13 2010 16:27

Usefull, thanks.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required