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.

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.

You might like these posts too::

  1. Sage Feed Reader
  2. Gmail RSS Feeds
  3. Google RSS
  4. WordPress Tip: Redirect to Previous Page After Login
  5. Remove qq_tracker_code_advanced_default Message from WordPress

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>