Limit Your WordPress RSS Feed To One Category

When designing Anderson Web Solutions, I decided to use Posts for both news items and Portfolio entries. There were several advantages to doing this, but one issue I found was that everything was being thrown together in the RSS feed. I decided to look into filtering the RSS feed to only show posts from the News category. Here’s how it works.

As with just about anything in WordPress, you can use Actions and Filters to customize the way WordPress RSS Feeds. I chose to filter the query because it already has built in category filtering functionality. First I hooked into the query filter.

add_filter('pre_get_posts', 'filterRSSQuery');

Then I created a function called ‘filterRSSQuery’ which does just what the name suggests.

function filterRSSQuery($query) {
	if ( $query->is_feed ) {
		$query->set('category_name', 'News');
	}
	return $query;
}

The code is pretty straight foreward, but here’s a breakdown. First we test to see if the query is for a feed (we don’t want to do this filtering anywhere else). Then we set the ‘category_name’ argument to ‘News’ (you would change this to whatever category you want to show up). Finally, you return the modified query object.

Overall, this is pretty simple, but extremely powerful.

19 thoughts on “Limit Your WordPress RSS Feed To One Category

  1. Pingback: WordPress and blogging articles for 18.03.09 | WPStart.org - WordPress themes, plugins and news

  2. Will Post author

    You would most likely put this in your functions.php file for your theme, but you could also put it in a plugin file. It would work either way (though in my mind the functions.php file makes more sense).

    Reply
    1. Will Post author

      Placement in the functions.php file is pretty flexible, but I usually try to place my newest changes at the beginning. The add_filter line should go after the function. Placing it directly after the function is the best idea because it makes it clear what’s going on.

      Reply
  3. Liam Dempsey

    Hi Will,

    Thanks for the great bit of coding. Very helpful.

    I wonder if you know how to tweak the code so that the RSS feed will include two categories rather than just one. I’ve tried a number of variations (including using an array), but I can’t seem to sort it out.

    Any ideas or guidance would be really appreciated.

    Many thanks!

    Reply
  4. Will Post author

    You can’t directly pass in multiple category names. You’ll have to pass in category IDs instead. This (untested!) code snippet should help:

    function filterRSSQuery($query) {
    	if ( $query->is_feed ) {
    		$cats = array();
    		$cats[] = get_cat_ID( 'Category 1' );
    		$cats[] = get_cat_ID( 'Category 2' );
    		$cats[] = get_cat_ID( 'Another Category' );
    		$query->set('cat', $cats);
    	}
    	return $query;
    }
    
    Reply
  5. Paula

    Hey this is just what I need thanks man! Except, I need it in reverse — I need to exclude one category and its children. I’ve been combing the documentation for over an hour and I can’t figure it out. Any suggestions?

    Reply
  6. rodney

    Hi Anderson !

    Your trick only_news_cat made my day, until I noticed it killed my top menu header… It doesn’t show up when the function is added in my functions.php template file. If I remove it, top menu is back.

    I almost had it… Any idea ?

    Thanks for the great work anyway

    Reply
  7. rodney

    A precision : The “topnav” which holds the wordpress generated main and only “header menu” show empty :

    Reply
  8. Courtney

    Hello,

    Thanks for this — this is exactly what I need. I use categories for many other things besides blog posts, but I really only want the RSS feed to allow access to blog posts. I’ve hardcoded the RSS icon link on the site… but I am now running into RSS autodiscovery issues.

    First of all — I have this in the functions.php:

    add_theme_support( ‘automatic-feed-links’ );

    This adds the necessary RSS feeds — but I want them to be filtered as your code suggests. However, I’m not seeing it happen; there are still non-blog-post items appearing. Should this code restrict what shows up in http://www.mydomain.com/feed/? I’m using the WP 3.4.1.

    Thanks!

    Reply
  9. Will Post author

    Hmm. I wrote this post about 3 years ago, so it’s possible that the API has changed. I’ll take a look later today and see if this breaks for me.

    Reply
  10. Courtney

    Thanks, Will. Yeah, I noticed it was from a while back, but seemed like there were recent comments. The reason I want to get this set up properly is because I want to use Feedblitz and it doesn’t work unless the feed is specified… I suppose maybe I could try hardcoding my specific feed instead. In any case, thanks for looking into this.

    Reply
  11. Will Post author

    Ok. Had a few minutes free, so I went ahead and tested. Running 3.4.1, and it seems to be filtering just fine on http://mydomain.com/feed/

    Couple things to check: did you add the right category name to the code snippet? If you have a non-existent category name, it won’t filter anything.

    Which links aren’t filtering correctly for you? Maybe there’s something in your setup that I’m not accounting for.

    Reply
  12. Courtney

    Thanks so much, Will. I’m still not seeing it working — I did check the names a few times before and I had it right. In any case, though, it turns out I can specify the specific category feed to Feedblitz, so I don’t need to rely up on a filtered /feed/ URL. So maybe the fact that this isn’t working for me is okay for now.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>