Picture of Mark Zahra

Mark Zahra

Mark has been involved with WP RSS Aggregator for almost a decade, from running customer support to now leading RebelCode, the company behind the plugin. He has a passion for content and news, especially the concept of ethically sharing content to help each other grow.

Table of contents

Streamline your
content-sharing process

Get started with WP RSS Aggregator today and unlock the full potential of RSS feeds!

RSS Tricks: How To Exclude a Category From an RSS Feed

There are a few reasons you might not want a category to appear in your RSS feed, and there might be a few ways to do it with plugins too, but this ‘RSS Trick’ will show you how to do it using just a few lines of code.

You might use a particular category just to display posts on your site in a particular way, or you might have a category on your site that isn’t even in use. You wouldn’t want these showing up in your RSS feed, so the following code will exclude them from your RSS feed.

The Fix

This is an example of how to remove one particular category from your RSS feed:

  1. First, you’ll need to find out the ID of the category you want to exclude.
    In order to locate this ID, head over to the Categories section of your WordPress site and click to Edit the category you want to exclude. The ID will then be shown in the URL:
    categories-id-location
  2. Once you’ve located the category ID you need, open your theme’s functions.php file. If it doesn’t exist, create it yourself.
  3. Place the following code into the functions.php file, changing the category ID (‘-70′) with the ID you just located. (Don’t forget to include the “-” before the ID number)
    function myFilter($query) {
        if ($query->is_feed) {
            $query->set('cat','-70'); //Don't forget to change the category ID
        }
    return $query;
    }
    
    add_filter('pre_get_posts','myFilter');
  4. Save the file.

How It Works

This works in a very similar way as the previous filters we’ve shown you in this ‘RSS Tricks’ series. It creates a custom function to exclude the category that you don’t want appearing in your RSS feed. It then uses the add_filter() function to apply it to the pre_get_posts() WordPress core function.

If you want to exclude more than one category at a time, you can do that too with a very simple modification. All you have to do is add a comma ( , ) and a dash ( – ) followed by the category ID, right after the ID you just entered. In other words, the line of code in question it would look something like:

$query->set('cat','-70, -46, -23');

Source: Zeo

Article by

Article by

Share the Post:

Leave a Reply

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

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

Related Articles