Exclude Category in WordPress

This is probably the fastest way to exclude category in WordPress besides using a plugin.

Dump this code anywhere in Theme’s template functions.php file (For example, functions.php file for a Theme named “default” would probably reside in the directory wp-content/themes/default/functions.php):

function exclude_category($query) {
	if ( $query->is_feed ) {
		$query->set('cat', '-5');
	}
return $query;
}

add_filter('pre_get_posts', 'exclude_category');

The code example above is for excluding category ID 5 from feed.

If you want to exclude multiple categories, simply add a comma ( , ) and a dash ( - ) followed by the category ID. For example:

'-5, -6, -7'

To exclude category from main page change is_feed to is_home. Or if you want to exclude for both feed and main page, then you would need to replace:

if ( $query->is_feed ) {

with these line:

if ( $query->is_feed || $query->is_home ) {

For more possible page conditions, please refer to the Conditional Tags page in the WordPress Codex.

36 Comments

Saracen

Just a question, will this preserve the number of entries received? As in, does it correctly ignore the excluded category posts from the post count?

Zeo

As far as I know yes. Go ahead and give it a try.

Ben

I wanted to exclude several categories from the main feed (front-site feed), but the single-feed for those categories should still be available. Your function works! I’m just wondering why the single feeds are not affected by that function. Could you explain that? Thanks a lot!

rkcorp

hey thanks this will definately help my work inprogress for custom made wp

der.steppenwolf

You can’t dump the function anywhere. I first inserted it at the end and it didn’t work. Putting it at the beginning everything was fine.

decompiler

thx, Safirul, this worked exactly as i hoped it would after reading your write up.

p34c3!

Bizzy

be easier to just add code to your theme functions, index.php

php:
if (is_home()) {
query_posts(”cat=-5?);
}

RickJW

Thought you had the answer for me there - Im running WP 2.1 and when I (finally) put the function at the start, some errors went away. But it doesnt work and at the same time it breaks the recent_posts that Im using in WP.

I spose its back to the drawing board for me - unless I want to 1st install widgets, just for one plugin?!?! :S

Thanks anyways! :)

decypher

doesn’t work for me. i want to hide multiple categories, but i think it hides only one.

Zeo

decypher, multiple categories only supported in WordPress 2.1 and above

decypher

thanks Zeo :)

Stephanie (mortaine)

That’s awesome! I’ve been looking for an easy way to exclude my Twitter posts from my main feed, as they clutter everything up, big-time! Thanks so much!

nooby

what is there is no functions.php in my theme? where should i dump this code to?

nooby

hello? can anyone help me?

Zeo

nooby, just create the PHP file (functions.php) yourself if none and place it in your theme folder.

nooby

cool. Thanks Zeo!!!! I just have to dump a functions.php with the code? that is right? :D

MrGroove

Nice Plug. Thnx for the contribution Zeo

nooby

i created a function.php, and it crashed my whole theme. word press error instead. anyone mind helping the nooby me?

thanks :(

Nick Prignano

Awesome. Works even with the latest version.

Rich

I’m trying to find a way to exclude a category from an RSS feed. I put the above code into a new functions.php file in my theme folder, and set it to the right category, but it didn’t work. Any advice? Is there perhaps a way to add some code directly to the category that I want to exclude?

junwei

can I use add_filter to filter pages and post? I need to display the search result in two view. one to display all posts and another display all pages. Izzit possible?

richard

My theme does not have a functions.php file.

Can I create one that contains just this code, or is there another solution?

richard

Sorry - previous post was answered up above, and I missed it.

So I’ve created a functions.php file, and it is having the effect of breaking the WP post editor! (WP 2.3.3)

When I try to save or modify a post, a page of errors results:

Warning: Cannot modify header information - headers already sent by (output started at /home/.goomba/calyxdesign/calyxdesign.com/wp-content/themes/modern/functions.php:15) in /home/.goomba/calyxdesign/calyxdesign.com/wp-includes/pluggable.php on line 390

I don’t even know if the RSS feed exclusion is working, because I can’t create a post to test it.

Any idea as to what’s happening here?

cr3

I love you so much.

nlx

Hi; Thanks for the article.

Personally i need to exclude a category from posts. How can i do that using your method ? My php knowledge are closed to zero… :/

Leave a Comment

(required)
(will not be published) (required)