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:

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

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

Comments RSS feed TrackBack URL

48 Comments

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

  2. Zeo

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

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

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

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

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

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

    p34c3!

  8. Bizzy

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

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

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

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

  11. Zeo

    decypher, multiple categories only supported in WordPress 2.1 and above

  12. thanks Zeo :)

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

  14. [...] Exclude Category in WordPress [...]

  15. nooby

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

  16. nooby

    hello? can anyone help me?

  17. Zeo

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

  18. nooby

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

  19. Nice Plug. Thnx for the contribution Zeo

  20. nooby

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

    thanks :(

  21. Hi,

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

    Cheers.

  22. Awesome. Works even with the latest version.

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

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

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

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

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

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

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

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

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

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

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

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

  34. Zeo

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

  35. [...] Exclude Category in WordPress [...]

  36. cr3

    I love you so much.

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

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

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

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

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

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

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

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

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

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

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

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

Leave a Reply

XHTML: You can use these tags: <li> <ol> <ul> <strong> <em> <code> <pre> <p> <br> <dl> <dt> <dd> <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite="">