Excluding one category from the main WordPress feed

Today I needed to exclude a category from the main WordPress posts feed, the one you’ll find at http://mywordpresssite.com/feed/; however I didn’t want to exclude posts in that category from other feeds (especially not from that category’s feed at http://mywordpresssite.com/excluded-category/feed/). Here’s how I set about it…

Here’s the code snippet for y’all: exclude photos from main feed

The action I’m using is pre_get_posts, which is fired before the WP Query object puts together or uses the DB query to get the posts, and after the query parameters have been established. This action is a perfect time to monkey around with the query parameters.

Before I get down to the monkey business, I run a series of checks to ensure that we are querying posts for a feed, and that it isn’t a category, tag or comments feed. (If anyone knows a way to positively test for the main site feed, rather than negatively testing that it isn’t one of the other feeds, please let me know.)

Once I’m sure I’m altering the right feed, I assemble an array of term IDs. I’m doing this by retrieving the category terms by slug, this ensures seems clearer to me than directly inputting term IDs as slugs are less likely to change between sites (so if you want to use this plugin on a number of sites then the code won’t need to change as much, and if it does change a slug is more obvious than an abstract number like a term ID). Once this is done, I use the set_query_var function to set the category__not_in var, which will ensure that no posts in the chosen categories are returned. (N.B. at the time of writing, the child categories of excluded categories are not checked… the posts excluded must be explicitly within the categories given in order to be excluded.)

That’s it… check through the code (link above) and let me know if you’d do anything different. Hope it helps.

One thought on “Excluding one category from the main WordPress feed

  1. Tony

    Thanks for sharing this code, it is close to what I am looking for. However, what I need is a bit different…and I think simpler code-wise. I would like to exclude a category from all feeds except the feed for that particular category. I was looking at your code, but can’t figure it out. Do you have a second to take a shot at it? Also, I would like to use the category ID instead of the slug to exclude it.

    Reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>