18 Nov 2006

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:


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

TrackBack URL

70 Comments

  1. Hi there friends, good article and fastidious urging
    commented at this place, I am really enjoyingg by
    these.

    My blog post – Heart Attack Stroke Prevention Center Spokane

    Reply

  2. I’m now not positive the place you are getting your info, however good topic.
    I must spend some time finding out more or understanding more.
    Thanks for magnificent information I used to be searching for this information for my
    mission.

    Reply

  3. We are passing along free samples of prime
    quality chocolates. To be qualified to apply, simply just reply to our comment together
    with your address and we will ship it out within the
    next day.

    Reply

  4. Using Ancient Rome 3D in Earth, you can explore Rome as it appeared in 320 A.
    But unfortunately not every request will get approved.

    The only tab of your concern is Public Templates, and no actions are necessary as it is already on the screen.

    Reply

  5. [...] WordPress : how-to exclude categories from a feed or try this alternate method. [...]

    Reply

  6. After weeks of working on my site to get this to work, this snippet finally did the job. Thanks so much!

    Reply

  7. Thank you very much for that code. I have an old version of wordpress and couldn’t find a working plugin for excluding categories from frontpage of my blog. Your function works like a charm…

    Reply

  8. jay

    Hey Zeo
    I have excluded multiple categories from my homepage. Now what I want to do is to create a navigation menu that will display all categories. Whenever I click on a button, only posts from that category should be displayed on the homepage. Is this possible?

    Thanks

    Reply

  9. [...] het is ook bekend dat een min-teken voor de waarde deze juist uitsluit, maar dat werkte dus niet. Dit blijkt alleen te werken met IDs. De volgende code werkt [...]

    Reply

  10. Well, sometimes you find solutions so great that you simply become speechless. I was banging my head for few hours and your posts saved many hours. I saw various people wrote lengthy code to do the job but yours was bang on target.

    Thank you very much and keep it up.

    Reply

  11. code modified? I need home page in a category id exclude :(( Help me!

    Reply

  12. Had my query_posts already setup and the exclude wasn’t working (cat=-x) cause of the get_options that I had enabled, plugged your little snippit in and BAM! Worked like a charm thanks man!!!

    Reply

  13. W/o the php tags:

    query_posts(‘cat=-1′.’&paged=’.$paged);
    // This code removes category 1.

    query_posts(‘cat=1,2,3′.’&paged=’.$paged);
    // This code shows category 1, 2 & 3.

    Reply

  14. I wanted to exclude a category with working pagination. After alot of struggle I ended up posting this code before The Loop in index.php (or any other template):

    // This code removes category 1.

    Or if you want to filter from three categories:

    // This code shows category 1, 2 & 3.

    Reply

  15. Kinda awesome that a tip from 2006 still works. Thanks!

    Reply

  16. Chris

    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?

    Reply

  17. [...] “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 [...]

    Reply

  18. great working even with many categories at 2.8.5 thank you

    Reply

  19. [...] 않도록 하려면 Advanced Category Excluder와 같은 플러그인을 이용하거나 Exclude Category in WordPress 글에 소개된 코드를 사용 중인 테마의 functions.php에 추가해서 특정 [...]

    Reply

  20. joe

    why not just use these plugins, find them in google by typing: “wordpress exclude category plugin”

    Reply

  21. Thanks a lot!! Really useful!!!

    Reply

  22. Thanks man, great hack and tip.

    Reply

  23. Error

    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

    Reply

  24. [...] Exclude Category in WordPress Friday, May 29th, 2009  Tags: bookmarks Category: Me [...]

    Reply

  25. 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.

    Reply

  26. 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! (:

    Reply

  27. How could I use this code to exclude _pages_ from wordpress?

    Reply

  28. Kyle

    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.

    Reply

  29. Scitt

    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.

    Reply

  30. Darcy

    Rich from Feb 5, 2008:

    Did you even find an answer to your question? I am having a problem with the same exact thing.

    Reply

  31. behrad

    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

    Reply

  32. [...] posé plusieurs fois au cours des derniers mois. Comme vous allez le voir la réponse fournie par Zeo est presque [...]

    Reply

  33. 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.

    Reply

  34. 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… :/

    Reply

  35. cr3

    I love you so much.

    Reply

  36. [...] Exclude Category in WordPress [...]

    Reply

  37. Zeo

    richard, refer to this http://faq.wordpress.net/view.php?p=7

    Reply

  38. 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?

    Reply

  39. My theme does not have a functions.php file.

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

    Reply

  40. [...] http://zeo.unic.net.my/notes/exclude-category-in-w... [...]

    Reply

  41. 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?

    Reply

  42. [...] 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 [...]

    Reply

  43. [...] 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′); } [...]

    Reply

  44. [...] to Zeo for this great [...]

    Reply

  45. [...] 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, [...]

    Reply

  46. [...] 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 [...]

    Reply

  47. 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?

    Reply

  48. [...] Exclude Category in WordPress (tags: webdev exclude plugin hacks categories php plugins wordpress) [...]

    Reply

  49. Awesome. Works even with the latest version.

    Reply

  50. Hi,

    I have two more ways to exclude specific categories from your blog on my blog.
    http://blog.gadodia.net/excluding-certain-categori...

    Cheers.

    Reply

  51. nooby

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

    thanks :(

    Reply

  52. Nice Plug. Thnx for the contribution Zeo

    Reply

  53. nooby

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

    Reply

  54. Zeo

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

    Reply

  55. nooby

    hello? can anyone help me?

    Reply

  56. nooby

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

    Reply

  57. [...] Exclude Category in WordPress [...]

    Reply

  58. 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!

    Reply

  59. thanks Zeo :)

    Reply

  60. Zeo

    decypher, multiple categories only supported in WordPress 2.1 and above

    Reply

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

    Reply

  62. 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! :)

    Reply

  63. Bizzy

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

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

    Reply

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

    p34c3!

    Reply

  65. 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.

    Reply

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

    Reply

  67. [...] WordPress : how-to exclude categories from a feed or try this alternate method. [...]

    Reply

  68. 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!

    Reply

  69. Zeo

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

    Reply

  70. 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?

    Reply

Leave a Reply