WordPress Tip: Remove a Category from the WordPress RSS Feed

I recently setup The Events Calendar WordPress plugin on a few sites for work. The events are added as posts, so they show up in the WordPress RSS feed.

We didn’t want events showing in the RSS feed, this is the code I ended up with:

// Keep events out of RSS
add_filter('pre_get_posts', 'exclude_category_from_feed');
function exclude_category_from_feed($query) {
	if ($query->is_feed) {
		$query->set('cat','-'.get_cat_ID( 'Events' ));
	}
	return $query;
}

Adding that bit of code to the functions.php file for your theme will prevent posts in the “Events” category from appearing in your RSS feed. You can obviously change “Events” to whatever category you want to exclude.
[ad]
It can be extended too, not just limited to keeping a category out of the RSS feed. For example, to keep a category off the home page or archive pages, you can change this:

if ($query->is_feed) {

To this:

if ($query->is_archive || $query->is_home) {

That’s it! Hope you find it useful.