Excluding Category from WordPress RSS Feed

 

There are a few WordPress plugins available that allow you to simply exclude posts from a certain category in your RSS feed, but I ran across the code that actually does the heavy lifting and wanted to share.

In your template directory, create a file (functions.php) if it doesn’t exist and place the following code in the file:

1
2
3
4
5
6
7
8
9
10
11
	function remove_press_releases($query) {
 
		if ( $query->is_feed  ) {
			$query->set('cat','-20');
		}
 
		return $query;
 
	}
 
	add_filter('pre_get_posts', 'remove_press_releases');

Make note to change the number to the catID (category id) of the category, or categories, you want to exclude.  This hook is called when preparing the posts to go to the template – so we are only filtering out if it’s the RSS feed in my case.

If you want a lot of heavy lifting without the coding (including the Google Sitemap XML integration, excluding categories/tags from search results and more) check out the ACE plugin.

Comments (0) Leave a Comment
  1. No comments yet.
Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
  1. No trackbacks yet.