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.
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?
As far as I know yes. Go ahead and give it a try.
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!
hey thanks this will definately help my work inprogress for custom made wp
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.
thx, Safirul, this worked exactly as i hoped it would after reading your write up.
p34c3!
be easier to just add code to your theme functions, index.php
php:
if (is_home()) {
query_posts(”cat=-5?);
}
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! :)
doesn’t work for me. i want to hide multiple categories, but i think it hides only one.
decypher, multiple categories only supported in WordPress 2.1 and above
thanks Zeo :)
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!
what is there is no functions.php in my theme? where should i dump this code to?
hello? can anyone help me?
nooby, just create the PHP file (functions.php) yourself if none and place it in your theme folder.
cool. Thanks Zeo!!!! I just have to dump a functions.php with the code? that is right? :D
Nice Plug. Thnx for the contribution Zeo
i created a function.php, and it crashed my whole theme. word press error instead. anyone mind helping the nooby me?
thanks :(
Awesome. Works even with the latest version.
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?
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?
My theme does not have a functions.php file.
Can I create one that contains just this code, or is there another solution?
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?
richard, refer to this http://faq.wordpress.net/view.php?p=7
I love you so much.
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… :/