How to Disable WordPress RSS Feeds or Exclude Categories from Them

When WordPress is running as a CMS, the feeds it publishes aren’t always wanted. Here are two ways to rein them in, neither of which touches a core file.

Disable the RSS and comments feeds

In using WordPress as a CMS, you may come across a situation where the RSS and comments feeds are unnecessary. To completely disable the feeds requires hacking the core WordPress files, which is something I simply refuse to do. Instead, we can use a clever little function to deny access to the feeds and hide them from the user.

All you need to do is add the following function to your functions.php file:

<?php
function disable_feed() {
    wp_die( __('Sorry, there is no feed available. Please visit our <a href="'. get_bloginfo('url') .'">homepage</a>.') );
}
add_action('do_feed', 'disable_feed', 1);
add_action('do_feed_rdf', 'disable_feed', 1);
add_action('do_feed_rss', 'disable_feed', 1);
add_action('do_feed_rss2', 'disable_feed', 1);
add_action('do_feed_atom', 'disable_feed', 1);
?>

Exclude categories from the 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 theme handbook.

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');
?>
Filed under:
Share

2 responses

  1. Chick Downtown Avatar
    Chick Downtown

    This formal post helped me very much! Saved your blog, extremely interesting categories just about everywhere that I see here! I appreciate the information, thank you.

  2. Thank you for that function.
    How can I modify it to disable only comments feed and retain posts feed (WordPress RSS)?

Leave a Reply

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