There are a few reasons you might not want a category to appear in your RSS feed, and there might be a few ways to do it with plugins too, but this ‘RSS Trick’ will show you how to do it using just a few lines of code.
You might use a particular category just to display posts on your site in a particular way, or you might have a category on your site that isn’t even in use. You wouldn’t want these showing up in your RSS feed, so the following code will exclude them from your RSS feed.
The Fix
This is an example of how to remove one particular category from your RSS feed:
- First, you’ll need to find out the ID of the category you want to exclude.
In order to locate this ID, head over to the Categories section of your WordPress site and click to Edit the category you want to exclude. The ID will then be shown in the URL:
- Once you’ve located the category ID you need, open your theme’s functions.php file. If it doesn’t exist, create it yourself.
- Place the following code into the functions.php file, changing the category ID (‘-70′) with the ID you just located. (Don’t forget to include the “-” before the ID number)
function myFilter($query) { if ($query->is_feed) { $query->set('cat','-70'); //Don't forget to change the category ID } return $query; } add_filter('pre_get_posts','myFilter');
- Save the file.
How It Works
This works in a very similar way as the previous filters we’ve shown you in this ‘RSS Tricks’ series. It creates a custom function to exclude the category that you don’t want appearing in your RSS feed. It then uses the add_filter() function to apply it to the pre_get_posts() WordPress core function.
If you want to exclude more than one category at a time, you can do that too with a very simple modification. All you have to do is add a comma ( , ) and a dash ( – ) followed by the category ID, right after the ID you just entered. In other words, the line of code in question it would look something like:
$query->set('cat','-70, -46, -23');
Source: Zeo