How to modify your WordPress RSS Feed

Tutorials, Wordpress Tutorial Apr 27, 2008 46 Comments

I for myself really enjoy using WordPress as a Content Management System, since most of the time its really easy to adapt to my needs. I usually use the different categories on my sites to display the various sections of the sites. For example, kriesi.at uses the category “tutorials” to feed the resources page and the “portfolio” category to feed my online portfolio.

This is easily accomplished by using the query_posts() function of WordPress which i won’t discuss in detail since the documentations are pretty comprehensive.

The problem I recently encountered is that the WordPress generated rss feed must be modified as well, otherwise it will display every post in each category. A user subscribing to my blog feed doesn’t want to be bothered with portfolio entrys, so I searched for a way to exclude categories from the main feed. There are basically two solutions I could find:

  1. Exclude the Categories via URL
  2. Exclude the Categories through using a small function

Both ways are really simple:

If you want to exclude a category you first have to know the ID of course. In WordPress versions prior to 2.5 you could just get it in your admin panels “manage” -> “categories” section. Post 2.5 versions do not display the category ID anymore but you can get it by hovering over the category name and extracting it from the link URL. Its the cat_ID attribute.

Say you want to exclude category 20 you just append ?cat=-20 to your feed url.

Example:

  • normal feed: http://www.kriesi.at/feed
  • modified feed: http://www.kriesi.at/feed?cat=-20
  • want to exclude more categories: http://www.kriesi.at/feed?cat=-20&cat=-21&cat=-22

Well these URLs are not what i would consider beautiful so you might use a service like feedburner to create a nice looking feed out of it. But beware, feedburner expects a slightly different syntax; insted of using ampersands you have to use commas:

So if you want to exclude a WordPress category from the Feedburner feed do it this way:

http://www.kriesi.at/feed?cat=-20,-21,-22

If you dont’t like this way of excluding you can use a small function instead. Just add the following code anywhere to your templates functions.php:

function my_cat_exclude($query) {
    if ($query->is_feed) {
        $query->set('cat','-20,-21,-22');
    }
    return $query;
}

add_filter('pre_get_posts','my_cat_exclude');

This function will strip the categories you define out of your RSS Feed without the need of adjusting your url.
A clean and simple way but not as flexible as using URLs if you want to provide more than one feed.

Hope this helps you if you ever need to modify your feed.

46 Responses to “How to modify your WordPress RSS Feed”

  1. Brandon says:

    Great to know, thanks for the info. :)

  2. Calaelen says:

    Will this work with feedburner rss-service?

  3. Kriesi says:

    yep, it does work with feedburner

  4. gopika says:

    Thank you very much.. 4 the valuable info…

  5. Promoted Profits says:

    Sweet, I can use this.

  6. Darren Littlejohn says:

    Hi,

    I understand how this works with query posts() because I’ve used that to exclude categories from my main blog page. I have a videoblog, photoblog, podcast and a static page for each of those where they appear as separate

  7. Darren Littlejohn says:

    But I don’t understand how to make ONLY select feeds show up….

    -d

  8. Doug says:

    This is awesome thank you!
    Any chance you have a version of that function that does the same if the query is for any archive listing on the site?

  9. Binh Nguyen says:

    This is really cool to know how to exclude categories from feed. Thanks for the tips.

    However, I’m actually looking for a solution to displaying latest MODIFIED posts, not the latest published posts. Do you happen to know some hack?

    Thanks again.
    Binh

  10. Kriesi says:

    Sorry, dont know, only way I do this is changing the time stamp on the post in the write panel…

    Dont’t know if there exists another solution…

  11. Peter says:

    Darren -

    To ONLY include a certain feed, just remove the – from in front of that feed number. It would also make sense to change the function name, just to prevent confusion. Here’s what I use:

    function my_cat_include($query) {
    if ($query->is_feed) {
    $query->set(‘cat’,’9′);
    }
    return $query;
    }

    add_filter(‘pre_get_posts’,'my_cat_include’);

    Kriesi – thanks for this, its just what I was trying to do myself, but none of my solutions were nearly this simple. Great work.

  12. Iaan says:

    Awesome! Exactly what i (and my client) was lookin for ;-)

  13. Jens Törnell says:

    It works great, thanks! =)

  14. Susan says:

    Thanks for the information. I need to remove a couple categories from my feed so I gave this a try. If you have a post that shows how to add images to feeds when you use custom fields…that would be awesome. I tried on of the WordPress plugins to add the images, but I couldn’t get it to work.

    Susan :D

  15. Nestor says:

    Wonderful, exactly what I was looking for. Feeds that give away too much can be troublesome

  16. Jack Dowson says:

    I have one question, its slightly off topic; I am using Snapshot theme designed by WOOTHEMES; however, recently I discovered that the images weren’t appearing inside the RSS Feed. Do you know what might be causing the problem? And how to solve it?

    I have tried several solutions but failed to figure out the issue. Let me know if you have any suggestions. RSS Feed to my blog is http://feeds2.feedburner.com/BloggerTemplatesBlog

    Thanks and Regards;
    Jack Dowson

  17. Jack Dowson says:

    Hey Admin!

    Just give me the reply of my above comment. If you can write a post on this, then surely you can help me out!

    Looking forward for your reply

  18. Mau says:

    Hi, thanks for the info. In my case it is somehow different. My post are custom-database driven, so when you open one post it queries from both wp and non-wp tables in order to show the content.

    What I would like to achieve is to deliver RSS with those custom queries, as of now my subscribers are recieving only the post header.

    If you could give me a clue that would be cool. Thanks again and cheers.

  19. Honey Singh says:

    Thanks for the suggestions and information’s but i’m working on way to display full feed for categories !
    Any pointers ?

  20. jamie says:

    how would you go about getting this little function of yours working? Where do you put it? does it need to be called?

  21. Online Car Insurance >> http://onlinecarinsuranceclaims.com/ says:

    [... - http://www.kriesi.at is other nice authority of advice. Online Car insurance claims [… -

  22. Dick says:

    Nice information. I implemented the instructions on my web hosting site and it works great.

  23. Carrie says:

    Is it possible to exclude post thumbnails from the RSS feed using a similar function?

    I’m using the atahualpa theme and I have the php for p75thumbnail inserted into my byline, so I figure I could just block the byline from displaying in the feed but am not sure how to do this.

    Thoughts or guidance?

  24. Mason Adams says:

    i always use feedburner to syndicate my blog posts to other subscribers.`;:

  25. Brody James says:

    i always use feedburner to syndicate my blog posts to other subscribers.`:-

  26. Poppy Scott says:

    RSS feeds are really great because you are always updated with the latest news or blog posts.~’`

  27. LED Grow Light ` says:

    feed burner is very useful for blog owners because it is easy to syndicate feeds using feedburner **’

  28. Laparoscopic Surgery : says:

    i always use feedburner for sharing the feeds of my blog, feedburner is really great.:,

  29. Bum Bag : says:

    RSS feeds are necessary for transmitting your blog updates to your readers or followers;*’

  30. Ashish says:

    We have come a long way since this solution now. Twitter is really the new RSS.