How to Exclude a Category from WordPress Blog

WordPress categoriesIf you need to exclude a category from the WordPress blog page and sidebar, you do not need to install a third party plugin and add extra administration overhead. All you need to do is follow this easy to follow step by step WordPress tutorial, and by simply modifying a file you will have the WordPress categories you want excluded in minutes. This tutorial also includes the sample code and was written specifcially for beginners or anyone without any coding experience.

Tutorial to exclude category from WordPress blog page

  1. Login to your WordPress dashboard and find the WordPress category ID of the category you would like to exclude. Read How to Find a WordPress Category ID to learn how to find out the category ID.
  2. Once logged in, also list down the name of WordPress theme you are using by navigating to the Appearance > Themes node.
  3. Connect to your WordPress using FTP and navigate to the theme’s directory. If the theme name is ‘default’, the directory of the theme is /wp-content/themes/default/.

WPProHelp WordPress tip: You can modify a theme file from the built-in WordPress Editor (Appearance > Editor). Though it is always recommended to do theme and other WordPress files updates offline (via FTP like explained in this WordPress tutorial) so you can test the change and easily revert back if something goes wrong, such as a WordPress whitescreen.

  1. Download the theme’s file functions.php (e.g. /wp-content/themes/default/functions.php). Before doing any changes to the file, make a backup of such file in case you need to quickly revert back the changes.
  2. Copy the below code and paste it in functions.php file, right before the ?> (at the bottom of the file). If you want to add a comment to recognize the code at a later stage, you can do so by using the ‘//’ signs at the beginning of the comment line.

PHP code to exclude WordPress category from blog

function exclude_category($query) {
if ( $query->is_home() ) {
$query->set('cat', '-xx');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
  1. Replace the xx (mentioned in line 3 of the above code) with the category ID you recorded in step 1 of this WordPress tutorial. Leave the minus ‘-‘ sign in front of the category ID, only replace the xx.
  2. Once ready, save the file and upload it back to your website. Refresh the page and you should notice that the excluded categories are not showing up in the WordPress blog.

Excluding multiple WordPress categories from blog

To exclude multiple categories from showing up in the WordPress blog page, simply add all of the categories ID in the same line from the code above (line 3) separated by a space as per the example below.

$query->set(‘cat’, ‘-124 -125 -126’);

The example code above would exclude the categories with ID 124, 125 and 126.

Comments

  1. Hi,
    Thank you so much for taking the time to write this up. I have been all over the internet trying to exclude one of the categories as I am using WooSlider using the Post function as I want the photos to show up with SEO friendly text/captions, but do not want those photos as posts on the Post page. Your code worked perfectly.
    Again, thanks so much!
    Cheers,
    Janell

  2. Question: can I load the code into my custom CSS area?

    My concern is if I add the code directly into my theme, the next update will was away any customization.

    • Hi Oz,

      Yes unfortunately it is something you have to update each time you update the theme. Having said that, the theme do not frequently change, especially the functions.php file. The easiest way to find if the functions file changed is to use some text file comparison tool and compare both versions of the file functions.php.

      If you have any further queries or need assistance regarding this issue, do not hesitate to get in touch with me directly on robert@wpprohelp.com.

  3. Thanks for the tutorial…

    I couldn’t get it to work…

    I found the category id and it was removing the “exclude” category from my homepage…so I tried another ID and when I tested it, the post disappeared…ok…hmmm Looks like User Error…

    What I need is to removing the category from the “category” widget…and a post…the reason for this is because…I set up a category called “exclude” and assigned the TOS, Privacy Policy etc. to this category.

    Now when I go to the site, the category “exclude” shows up in the “category” widget…

    How would a Pro like you do this, ie., exclude a category from the “category” widget?

    Thanks in advance,
    Steve

    • Hi Steve,

      There are plugins which you can use to exclude categories from a Category widget. Alternatively you can add this code to the theme’s file functions.php to exclude the category from the category widget;

      < ?php
      function exclude_widget_categories($args){
      $exclude = "ID,ID,ID"; // Specify the ID of the categories you want to exclude
      $args["exclude"] = $exclude;
      return $args;
      }
      add_filter("widget_categories_args","exclude_widget_categories");
      ?>

  4. Thanks for the code been looking for something like this. I have a specific category that I´ve created a category-slug page for, so I dont want it to show in the normal archives page. Can I edit this code to get that to work, or does this just work with the homepage?

    • It is quite difficult to say unless we look into your setup. Drop us an email on support@wpprohelp.com and we will gladly assist you. If need be we simply write a new small function specifically for your setup.

      Looking forward to hearing from you.

      • thanks for the offer. I managed to solve it. this stops the cat being shown on all archive pages.

        function exclude_category( $query ) {
        if ( $query->is_archive() && $query->is_main_query() ) {
        $query->set( ‘cat’, ‘-388′ );
        }
        }
        add_action( ‘pre_get_posts’, ‘exclude_category’ );

        Now all I need to work out is how to add cat 388 back in on the specific template. hehe

        • Glad you solved it :) As regards your last query I didn’t understand. Maybe you can explain in more detail?

          • I have one category (bios) that I want to display differently to the rest of the site. So I removed bios from the archive pages with the code shown in my post above, and I set up a specific Archive Template for just that one category. I now just need to solve how to show the bios on that one page. Because atm they dont show because of the pre get post I set in functions.

            I can´t seem to find anwhere if I can use a specific category in this call
            if ( $query->is_archive() instead of archive.

            or maybe as I removed the cat wit pre get posts I should add it back in just on that page in the loop with get query.

            I suppose the other option is just to set up a custom post type for the bios.. probably easier. I´m working off the hybrid framework, so not all the info out there works with the theme anyway. Haven´t looked into the custom post options on the framework yet.

          • Hi Sylvia,

            Use conditional statements and use both is_archive and is_category and you should be able to display the archive of a specific category. I am sure you can find more information on the WordPress Codex.

            Alternatively you can use a plugin to achieve what you want; http://wordpress.org/extend/plugins/wp-category-archive/

  5. Thanks! It works like a charm!
    So nice to have this feature.

Speak Your Mind

*

 
facebook twitter rss