October 20, 2008

All, PHP, Tutorials, Wordpress

4 Ways to Exclude WordPress Category from RSS Feeds

Article written by Jeeremie

Lately, I wanted to exclude a few categories from WordPress Feeds. A search on WordPress didn’t return any valuable information. So, I searched the Google database. Here’s what I found:

1- Change the feed URL

The most common and maybe easier way to do so is to change your feed structure. In the default WordPress theme, the RSS link (in footer.php) is:

1
<a href="<?php bloginfo('rss2_url'); ?>">Entries (RSS)</a>

You can exclude any category by appending a query string to the feed URL, like this:

1
<a href="<?php bloginfo('url'); ?>/feed?cat=-3&cat=-12">Entries (RSS)</a>

12‘ in this example is your category ID. In older version of WordPress, the category IDs were displayed in Manage -> Categories, next to category name. However, in WordPress 2.5+, the WordPress Gurus removed this feature (only god knows why). To identify the category ID, mouseover the category name to see its ID in the status bar of your browser. Otherwise, you can install Reveal IDs for WP Admin 2.5 plugin to reveal the post, page and category IDs.

A more common PHP structure might work as well:

1
<a href="<?php bloginfo('url'); ?>/feed=rss2&cat=-3,-12">Entries (RSS)</a>

If you are using RSS2, your code will be:

1
<a href="<?php bloginfo('url'); ?>/feed=rss2&cat=-3&cat=-12">Entries (RSS 2)</a>

Depending on your permalink structure, you might need to add index.php to your feed url:

1
<a href="<?php bloginfo('url'); ?>/index.php?feed=rss2&cat=-5">Entries (RSS 2)</a>

2- Exclude categories in your template’s functions.php file

Jangro explains how to exclude a category from feed by adding a few lines of code to the template’s functions.php file:

1
2
3
4
5
6
7
8
function myFeedExcluder($query) {
 if ($query->is_feed) {
   $query->set('cat','-12');
 }
return $query;
}
 
add_filter('pre_get_posts','myFeedExcluder');

This code will keep the category ID=”12″ out of the feed. If you want to exclude more than one category, put them in separated by commas ‘-12,-25,-33′.

3- Add a plugin to WordPress

If you are not into coding, you can install the Ultimate Category Excluder plugin. It allows to exclude any categories from your front page, archives, and feeds. Once you installed it, go to the Category Exclusion page in your admin panel to exclude a category by selecting one. That is that easy.

You can also install the Stealth Publish plugin to exclude a specific post from being published in feeds.

4- Exclude category from RSS feeds in FeedBurner

If like me you are using FeedBurner, go to dashboard and click “Edit Feed Details”. In the field “Original Feed”, enter the feed URL with the categories you want to exclude as explained in first step above.

Conclusion

The methods described above would be interesting to implement if you write about two (or more) different topics on your site. Let’s say you write about Linux (which would be ID=”1″ in our example below) and Windows (ID=”2″). Some of your readers might want to read your Linux articles but not the Windows ones, and vice-versa. Thus, you could create two feed links, the first one excluding Windows feeds for your Linux reader and the second one the Linux feeds:

?View Code HTML4STRICT
1
2
<a href="http://yourdomain.com/feed?cat=-2">Subscribe to Linux Feed</a>
<a href="http://yourdomain.com/feed?cat=-1">Subscribe to Windows Feed</a>

The Author

Article written by Jeeremie:

Hi, My Name is Jeremie Tisseau. I am a French UI/UX Designer, Event Organizer and Web Entrepreneur based in Bangkok, Thailand, since January 2009. I design beautiful and functional web and mobile apps for early stage startups.

Want to become a guest author on this blog?

22 Comments

  1. Jeffro2pt0 says:

    24 Oct, 2008

    Question regarding this action. Would any of these 4 methods allow me to import a WXR file that contained a few categories from showing up in the feed reader? I don’t want my current subscriber base of 500 to be bombarded with new posts when I import a WXR file into my existing installation.

  2. Peter S says:

    9 Jan, 2009

    Thanks so much. It is just what I’m looking for :)

  3. Darren Sproat says:

    24 Mar, 2009

    THANK YOU!
    I have been looking for category exclusion code for controlling what is displayed on the default/home/front pages for what seems like ages! Like you, a search on wordpress didn’t turn up the results I was looking for… a move to google found your site and the info should allow me to do what I need to do…

    Thanks again,
    Darren Sproat

  4. Waterman pens says:

    1 Apr, 2009

    Woww, Thank you brother . Very good theme .

  5. Prearlinuri says:

    17 Apr, 2009

    nice, really nice!

  6. Steve G. says:

    25 Jan, 2010

    Thanks for sharing. That’s the tutorial I’m looking for!

  7. thank you for this exclude categories, but i want to exclude the main default one…. thank you :)

  8. Bad Bone says:

    26 Feb, 2010

    Nice article..I tell my friend to read this and bookmark..

  9. Allen says:

    13 Jul, 2010

    Thank you. I’ve been trying to hide my slider and photo gallery category rss feeds by using plugins, but they caused more confusion. Finally I found your article and after a while, got it to work perfectly.
    Appreciate your help…. too bad I’m gonna get all the credit for this. :)
    Thanks Jeremie.

  10. Adam says:

    25 Nov, 2010

    What if you have a post in 2 categories? It seems like if you exclude 1 of the categories the post is in, it will still appear in the feed.

    Would you HAVE to exlcude both categories or is there another way to hide that post?

    Thanks,

    Adam

  11. Online Blend says:

    25 Nov, 2010

    Excellent post, I love looking up how to do something and finding 4 different ways to do it so I can choose the best for me :) Cheers!

  12. Andrei says:

    24 Dec, 2010

    Great tip. Thank you for the code in the functions file. I hate plugins, so that solution rocks.

  13. Kim says:

    4 Feb, 2011

    Please update your article!

    The code method to exclude multiple categories has changed to the following format:

    feed/?cat=-1,-2

    For reference, see this ticket in the WordPress Trac:
    http://core.trac.wordpress.org/ticket/16387

  14. giona says:

    28 Feb, 2011

    Thank you, very useful!

  15. daniel mamann resort says:

    29 May, 2011

    Hello would you mind stating which blog platform you’re working with? I’m going to start my own blog soon but I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something unique. P.S Sorry for being off-topic but I had to ask!

    • Jeeremie says:

      31 May, 2011

      WordPress. You can do anything you like with WordPress, Drupal… It is up to you (or the developer if you hire someone)

  16. ThisIsWhyImBroke says:

    3 Jun, 2011

    I tried this code and it didn’t alter the category posts already in the RSS feed.. is it supposed to only work for new posts in a category that I want to exclude or should it affect the older ones too?

    • Jeeremie says:

      6 Jun, 2011

      Good question. I am not sure about this. I think it only affects the new ones.

    • Tivar says:

      1 Sep, 2011

      The old posts may are(was?) in the feed-cache from wordpress. If I change something in my feed I normally add this line in the theme-functions.php

      add_filter( ‘wp_feed_cache_transient_lifetime’, create_function( ‘$a’, ‘return 0;’ ) );

  17. Eric says:

    10 Nov, 2011

    Thanks so much! This was right on time. Still figuring out WordPress so your info was a great help! :) thanks!

  18. lougie says:

    26 Jun, 2012

    salamat!! ive been looking for this!

  19. victorkurauchi says:

    3 Aug, 2012

    Just awesome code! Helped a lot. Thanks.

Leave a Reply

Sorry, comments are closed