How to exclude a category from your main wordpress index and RSS feed

Few days ago I was experimenting with Twitter tools, so I found that the Tweet Digest posts created by Twitter tools are being listed with my other posts in the main index of my blog. Twitter digests posts were not looking good to me as the Twitter digests posts from Twitter Tools were long and they were making my other posts unfindable. So I excluded that Category in the main index listing of my blog. But just yesterday I added my blog to FriendFeed and this is when I figured out that I should also be disabling the ‘Twitter Archive‘ category in the RSS feed of my blog. So I did some research and found very helpful trick from wpbeginner. Below I will show you how I used the code from wpbegginer not by just copy-pasting it but by tweaking to fit my need. Before you read further let me just remind you what I am trying to do here. I am trying to disable ‘Twitter Archive’ category (with category id 27) in the main blog listing as well as in my default RSS feed. So to simply exclude the Category from RSS feed as mentioned on wpbeginner I had to include the following code in my theme’s functions.php.

function catExclude($query) {
    if ($query->is_feed) {
         $query->set('cat','-27'); //Don't forget to change the category ID
    }
    return $query;
}
add_filter('pre_get_posts','catExclude');

But the above code proved to be not working for my case, as I wanted to exclude the category without breaking customized feed or category based feeds of my blog. As the above code is explicitly setting category with category ID ’27’ to be excluded, it doesn’t matter if you use the customize feed URL to exclude other categories. In simple words the above filter does not preserve the values of cat in the query string, it just sets the new value regardless what was provided in the url to get the customized feed. So I just can’t use this function, being a perfectionist I don’t like breaking things to build or do something new. So I did some more research on the above code and found that I can do play with $query object that is being passed to catExclude function above.

After reading about the $query object of class WP_Query I found that there is a function which allows to get the value of current variable in query string. So I just added an if condition to the above code to check if there is already a value set for ‘cat’ in query string, if there is already a value set then I don’t modify the value, else I just exclude my ‘Twitter Archive’ category. By using this check of an existing cat value I successfully excluded ‘Twitter Archive’ category form default RSS feed with out breaking the functionality and I can still have separate feeds for all my categories. Not only that but my ‘Twitter Archive’ category can also have its own dedicated feed without showing up in main default RSS feed. Below is the final code that I used to exclude the ‘Twitter Archive’ category from my RSS feed.

function catExclude($query) {
    if ($query->is_feed) {
       if (!$query->get('cat'))  {
            $query->set('cat','-27'); //Don't forget to change the category ID
       }
    }
    return $query;
}
add_filter('pre_get_posts','catExclude');

Now there is one last thing I modified in the above code to use it to exclude category in the main index of my wordpress blog. As I mentioned earlier about my previous post on the topic, that describes another way of achieving the same thing. But in case you don’t want to use that method and want to keep your edits to the single file and exclude your selected categories from both the main index and RSS feed then you use the below code.

function catExclude($query) {
    if ($query->is_feed || $query->is_home) {
       if (!$query->get('cat'))  {
            $query->set('cat','-27'); //Don't forget to change the category ID
       }
    }
    return $query;
}
add_filter('pre_get_posts','catExclude');

I hope you already have noticed the use of is_home in if condition. If you will use the code above with is_home condition then you don’t need to use the method that I mentioned in my previous post about excluding category in main index of wordpress blog. Now the last thing you might want to do with the above code that is to exclude multiple categories. Specify the multiple categories as below in your $query->set call.

$query->set('cat','-27,-30,-32');

I know the above code samples might not make much sense to non PHP programmers, so if you need to edit your wordpress blog and does not know how, you can hire me. You can also find my contact information on the contact page.

If you know a better way of doing the same thing, kindly share with us in the comments. Even if you don’t know a better way, let me know in comments what do you think about this trick and if you like it then don’t forget to share it with your friends.

Published by

Hameedullah Khan

VR enthusiast, Cloud Architect, Passionate Programmer and Opensource advocate.

5 thoughts on “How to exclude a category from your main wordpress index and RSS feed”

  1. I think there is also a plugin for wordpress to do this.

    [Reply]

    Hameedullah Khan Reply:

    Yes definitely there is a plugin with advanced options. the reason to use the custom code is your blog will not have to load a whole plug-in. The functions.php gets loaded anyway with your theme.

    [Reply]

  2. Hi! This is my 1st comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading your articles. Can you suggest any other blogs/websites/forums that cover the same topics? Thanks!

    [Reply]

    Hameedullah Khan Reply:

    I am glad that you like reading the articles here. I will share some of the blogs that I read sometime soon.

    [Reply]

Leave a Reply

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

CommentLuv badge