How to Exclude Categories from Your WordPress RSS Feed

If you use WordPress as a CMS, you may find it useful to exclude certain categories from your RSS feed. For example, I felt that it was only logical to exclude the ‘work’ category from the RSS feed for this site. In doing so, I was able to manipulate WordPress into functioning more like a true CMS, separating my ‘work’ posts from my ‘blog’ posts.

All that is required is a simple function that tells WordPress to exclude the category from loop. Sounds easy, right? The trick is to specify the proper conditions under which you want WordPress to exclude the category. If you are unfamiliar with the WordPress loop, you can start learning about it here in the WordPress Codex.

Just add the following code anywhere in your themes functions.php file:

<?php
function cat_exclude($query) {
    if ($query->is_feed) {
        $query->set('cat','-3');  // to exclude more categories just separate them with a comma, ie. -1,-2,-3
    }
    return $query;
}
add_filter('pre_get_posts','cat_exclude');
?>

Here is another variation of this function that may seem a little more straight forward to the less PHP savy, myself included:

<?php
function cat_exclude() {
    if ( is_feed() ) {
       set_query_var('cat', '-3');
    }
    return;
}
add_filter('pre_get_posts','cat_exclude');
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you in need of a freelance web developer?

Hire me