How to Exclude Category Posts in WordPress – Tutorial with Code that Works

It is astounding how much worthless advice can be found on the web. If you are not a seasoned coder, you can find yourself facing simple dilemmas which can turn into a nightmare if solutions offered on various websites don’t lead to actual solution of your problem, only cause more problems and frustration. And that’s what I found to be the case with excluding posts from one particular category (or more categories) from your WordPress powered blog’s main page. I have been contacted by several people asking me for help excluding category posts from their WordPress blog, each of which was tearing their hair out as their tried many suggested solutions you can find across the web, but they either don’t work at all, or exclude the category, but break something else (mostly bust your title tags which are the most important part of your on page SEO).

Why Exclude Categories in WordPress?

There are several reasons why you may need to exclude category posts from your WordPress blog. One of the WP blogs I have built used a “Video of the Day” feature. The blog normally focused on different subject matter, but the owner wanted to offer his readers an extra feature to keep them entertained so he introduced the Video of the Day page. He would be posting one cool video each day and had them all listed on a dedicated page, however he didn’t want these Video of the Day posts mixed up with main topic of his website. So he was looking for a code to add to the WordPress theme which would exclude all posts from Video of the Day category from his main blog page. It turned up being a pain in the neck finding out the tutorial or ready to use code that would safely and reliably exclude that category.

How to Exclude Category Posts in WordPress – Tutorial

To exclude posts from a particular category in WordPress you first need to create that category and then mark down the category ID. It is very easy to do, just follow these steps:

  • go into Posts > Categories
  • create New Category (skip this step if you are excluding a category which already exists)
  • click on Category you want to exclude
  • mark down the ID number of the category – you can see it by taking a look at the ULR of the page in your browser’s address bar. It is the number at the end of the URL, right after the equal sign. The URL should end with something like this &cat_ID=xx where xx is a number (note, it could be a single, double or triple digit number, depending on how many categories your WordPress blog has. Some blogs could get four or five digit number. Just mark down the number in full as it appears after the equal sign)
  • go into Appearance > Editor and click on the file called “Theme Functions” (functions.php) listed under the Templates on the right hand side
  • paste the code below into your functions.php theme file, right before “?>” part at the end of it, replace “xx” with your category ID you have made note of and click “Update File”.

Code to Exclude Category Posts in WordPress

Use this code to paste in your functions.php file. It should be pasted right before the ?> part which should be the part of the exiting code. Replace “xx” in the code provided with Category ID you have made note of before hand. This is absolutely essential as you must let WordPress know which category you want to exclude for it to work. If you forget about it and paste it as it is provided below with xx instead of a Category ID, your category poses will NOT be excluded! Make sure you keep the minus sign (-), only replace “xx”, nothing more.

function exclude_category($query) {
if ( $query->is_home() ) {
$query->set('cat', '-xx');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');

Excluding Multiple Categories in WordPress

Sometimes you may wish to exclude more than one category from your main blog page. To do it is again very easy, you just need the Category ID of each category you wish to exclude. Post that Category ID along with the minus sign right before it in the same space as other category IDs, but separate them with spaces. So the final code would look something like this:

$query->set('cat', '-9 -53 -98 -484');

An example of code above would exclude posts from categories with following IDs: 9, 53, 98, 484

How to Exclude Categories from Archive Pages in WordPress

Sometimes you may also want to exclude category posts from archive pages. Or you may wish to have a category excluded from both Archives and Main Blog Page. Both of these are easy. The code for it would be as follows:

To exclude category posts from Archive pages, replace second line of code with following code:

if ( $query->is_archive ) {

To exclude category posts from both Archive pages and Main Blog Page, replace second line of code with following code:

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

That’s it. I have written this post in a way so even a new blogger could understand it and hopefully it helps. These codes definitely work with WordPress 2.7 through 2.8 but will also work on most older versions of WordPress.

 

Incoming search terms:

  • wordpress exclude category
  • exclude category wordpress
  • exclude category
  • wordpress exclude category from blog
  • exclude categories wordpress
  • exclude category from blog wordpress
  • wordpress exclude categories
  • exclude posts wordpress
  • query post exclude category
  • exclude categories

70 thoughts on “How to Exclude Category Posts in WordPress – Tutorial with Code that Works

  1. SLT-A77

    Io senza dubbio desiderato a salire un riconoscimento per essere mezzo per apprezzare abbiamo per tutti un contributo glorioso siamo stati qui esposti. Il mio curiosare internet allargata è subito stata famosa con grandi strategie di andare oltre con i miei amici e la famiglia. vorremmo dimostrare che la maggior parte di noi visitatori essenzialmente sono stati senza dubbio santificato ad esistere in un villaggio cospicuo di persone davvero più diluito con sentori redditizio. ci sentiamo veramente grati di avere rilevato le tue pagine web così come atteggiamento sfacciato per la maggior parte in modo qualcosa di più straordinaria festa minuti di una massa qui. Grazie ancora per noi tutti dettagli.

    Reply
  2. Ciaran

    Thanks for this, very useful.

    I made a slight alteration to the code so that I can still see all the posts in the category if I click that category’s page:

    if ( $query->is_home() || ($query->is_archive() && !$query->is_category())) {

    (For me the is_archive() check was also returning true on the category page)

    Reply
  3. MP

    Awesome! I was having the same issue with posts not showing in the category’s page. Ciaran’s mod did the trick. Thanks guys!

    Reply
  4. sanchit

    What if we want to exclude a category from timeline page template..the latest news page on my website(jeeexam.com) is a timeline template which shows all the posts from all the categories,,,plz tell me a way so that i can exclude certain categories from that page thanks..

    Reply
  5. Monika

    Thanks. It’s exactly what I was looking for desparately and it works perfectly in just the way you described in the newest WordPress version.

    How nice of you to post this!!!!
    Monika

    Reply
  6. MT

    As old as this article is, turns out it’s still very relevant. I’ve been searching for the longest time for a solution that would allow a “non-coder” a way to prevent a particular category from showing in a listing of posts. After much frustration and days and days for looking/hacking/modifying/etc…I found this article and it works like a charm…thank you Mark!!

    Reply
  7. Roxanne

    Thank you so much,

    I wasted about 1 hour trying to solve this issue reading on other website and with useless plugins. It took me 5 minutes using your method, it is so simple and very well explained.

    You did a really good work, I’m going back to your website for sure.

    Have a nice day

    Roxanne :-)

    Reply
  8. Skip

    I have this code in my functions.php file and it works beautifully. Is there any way at all it could be adapted so that if a post is in 2 categories, only one of which is excluded, it still gets included? So in your example I’ve excluded category 53 but the post is also filed under category 54 and that should take priority?

    Reply
  9. Kim

    Just in case someone runs into the same problem that I did —

    There was no ?> in my functions.php file. So after trying a few places that didn’t work, I put it at the very end, and it worked!

    Thanks!!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>