Include or Exclude Category for RSS Feed or Search

Include or Exclude Category for RSS Feed or Search

There are certain times when you need to include or exclude certain categories in the feed or search results. The easiest way to take care of this is to add the following to your functions.php file.

function sdac_remove_from_feed( $query ) {
	if ( $query->is_feed ) {
		$query->set( 'cat',-1 );
	}
        return $query;
}
add_filter( 'pre_get_posts','sdac_remove_from_feed' );

The code above will exclude all posts that are in the category with ID 1 from your feed. If you wanted to exclude everything from the search results – you would change one line:
$query->is_feed to $query->is_search

If you wanted to only show items from the category with ID 1 – you would just need to remove the “-” before the category number.

Note: You can include/exclude items tagged, etc by changing the ‘cat’ to whatever else you would normally use in the query.

16 thoughts on “Include or Exclude Category for RSS Feed or Search”
  1. Tibi |

    Excellent tutorial man. I just implemented this on my blog so i can skip some categories from getting parsed on my RSS.

Leave a Reply

Your email address will not be published.
*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.