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.
Pingback: WordPress and blogging articles for 18.03.09 | WPStart.org - WordPress themes, plugins and news
Hi Will,
Can you elaborate on how and where to include this? What files?
Thanks,
Matt
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).
Thanks for this! It works great!
Hey there
I was wondering where you would put this in the functions.php file and would the add filter line need to be right next to the query code you put?
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.
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!
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; }Hey Will,
Cheers for the quickly posted reply. Much appreciated!
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?
naturally the very first page I came to when I left here had the answer. No need to moderate this for my sake — the URL I found is here in case you care to share: http://web-kreation.com/all/4-ways-to-exclude-wordpress-category-from-rss-feeds/
Thanks again!
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
A precision : The “topnav” which holds the wordpress generated main and only “header menu” show empty :
I can’t duplicate that issue. What version of WordPress are you using, which version of the code did you copy into your functions.php file, and are you using custom menus?
Something that might solve your problem is using the new is_main_query() check.
http://wpdevel.wordpress.com/2011/12/07/new-api-in-3-3-is_main_query/
However, since I can’t duplicate the issue you described, I can’t really test to see if that function helps
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!
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.
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.
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.
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.