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!
[...] WordPress : how-to exclude categories from a feed or try this alternate method. [...]
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!
[...] Exclude Category in WordPress [...]
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 :(
Hi,
I have two more ways to exclude specific categories from your blog on my blog.
http://blog.gadodia.net/excluding-certain-categori...
Cheers.
Awesome. Works even with the latest version.
[...] Exclude Category in WordPress (tags: webdev exclude plugin hacks categories php plugins wordpress) [...]
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?
[...] clean (it’s imported into many social networks like mugshot & facebook) I’ve used this post to filter out my new “sync” category… sync as in I’m synchronising my web [...]
[...] it out, Zach. I finally figured out how to exclude my tweets from generic blog-feed! Thanks to This post which google fed me. So, as the only person (I think) who subscribes to both my tweets and my blog, [...]
[...] to Zeo for this great [...]
[...] Zeo indique que cette manip permet aussi de ne pas afficher les articles de certaines catégories sur la homepage de votre blog. Il suffit de faire ces adaptations : function myFilter($query) { if ($query->is_feed || $query->is_home) { $query->set(‘cat’,'-5′); } [...]
[...] Exclude Category in WordPress Como excluir categorias del loop de Wordpress sin tener errores en la paginacion. (tags: wordpress categories howto) Archivado en: Links Diarios | Etiquetas: No Tags. Search [...]
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?
[...] http://zeo.unic.net.my/notes/exclude-category-in-w... [...]
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
[...] Exclude Category in WordPress [...]
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… :/
This helped me get closer, but let’s say I want to exclude only the most recent post from a specfic category in one feed because it’s being featured in a different section of the site? I’m trying to avoid duplicate posts on my front page while maintaining the rest of the category’s visibility. Is there any way to do this?
For example: http://www.playerfive.com
I have a “Featured Article” that has a listing all of it’s own. I want it to remain there, but not show up in my main feed which just lists all posts by date.
[...] posé plusieurs fois au cours des derniers mois. Comme vous allez le voir la réponse fournie par Zeo est presque [...]
hey Zeo.
ive already excluded the categories, but the main question is how to use excluded categories in another pages ?
i mean, if you have IT and DATA category by using the category exclude you gonna ignore them in main page, so how can we show those categories in another particular pages ?
Thanks
Rich from Feb 5, 2008:
Did you even find an answer to your question? I am having a problem with the same exact thing.
Thanks for this. This looks like it works universally except for with category views ($query->is_category). With categories, weird things happen – With this code enabled, I see all posts from all categories regardless of which category I try to view. Looks like categories handle the query differently – like they’re not applying the pre_get_posts filter soon enough.
I had the same problem as Scitt. I even tried to wrap the filter code in an if statement so it wouldn’t even appear on a category page and it still didn’t work.
How could I use this code to exclude _pages_ from wordpress?
Thank you, thank you, THANK YOU for this post. I spent the last three hours trying to do this for a client and could find virtually nothing on the WordPress site. I’m only a beginner when it comes to PHP, so figuring this out would have taken me hours of valuable time.
You rock! (:
Excuse my lack of PHP knowledge, but…
How do I change the “is_feed” into “is_page(‘37′) ?
I don’t see the parentesis in your “is_feed” so I don’t know how to implement it here.
[...] Exclude Category in WordPress Friday, May 29th, 2009 Tags: bookmarks Category: Me [...]
Warning: Cannot modify header information – headers already sent by (output started at /home/g70910/public_html/wp-content/themes/novogn/functions.php:1) in /home/g70910/public_html/wp-includes/pluggable.php on line 850
[...] 않도록 하려면 Advanced Category Excluder와 같은 플러그인을 이용하거나 Exclude Category in WordPress 글에 소개된 코드를 사용 중인 테마의 functions.php에 추가해서 특정 [...]
great working even with many categories at 2.8.5 thank you
[...] “Google is your friend!” heb ik even op google gezocht en kwam ik uiteindelijk bij deze pagina Hier wordt omschreven wat er moet gebeuren om een categorie niet mee te nemen tijdens het zoeken [...]
Hey,
This is very helpful. Is there a way that the posts can also be excluded in the wp_calendar and from the next_post_link and previous_post_link results?