The easiest way to exclude categories from RSS feed

From the moment when you publish your first post using WordPress, your new posts will automatically be published as RSS feed. That feed can be easily accessed by any user who wants to follow your website, and because of that, you need to make your RSS feed as good as it can get.

We already have shown you how to quickly enable images in your feed, how to show specific content only in RSS feed and even how to display any external feed or use one in your admin panel. But sometimes, you will need even more control over your RSS feed.

While you can easily postpone publication of your feeds by anytime you want, sometimes you will need to show only specific categories to your followers. Better yet, you will want to exclude those categories that you don’t want to be displayed in the feed.

Easily exclude categories from RSS feed:

For example, you might have a dozen of categories which are being updated with new content on a daily basis and you don’t want to overwhelm your users. Maybe a specific category is designed only to draw in users from social media sites. Or maybe you want your RSS feed only to show category “news”. Whatever the reason, in the following lines we’re about to show you how to easily exclude a category from RSS feed:

  1. Open functions.php
  2. Copy and paste the code:
  3. function exclude_categories_from_rss($query) {
    if ($query->is_feed) {
    $query->set('cat','-10,-21,-32');
    }
    return $query;
    }
    add_filter('pre_get_posts','exclude_categories_from_rss');
    
  4. Change category ID’s on the 3rd line
  5. Save changes

The easiest way of finding a category’s ID number is opening a category from Posts -> Categories. Once you click on “Edit” button, the ID will be shown on top of your browser in a form of URL.

As you can notice from the code, to exclude a category you will need to enter a “-“ character before the ID. Once you set the IDs, those categories will no longer be displayed in your RSS feed. If you need to change that in the future, you can simply change the function when needed and add/remove a category from the list.

Your RSS feed has just become even better. Don’t forget to check our other tutorials on how to improve your WordPress-powered website and make the best of already great platform.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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