Home Blog WordPress Management How to Exclude a Category from a WordPress Blog or Page

Excluding a Category from WP Blog or Page

How to Exclude a Category from a WordPress Blog or Page

If 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/.

WP White Security 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.


23 thoughts on “How to Exclude a Category from a WordPress Blog or Page

  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.

    1. 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@melapress.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

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

  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?

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

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

  5. Hi, nice function!

    But this function excludes also the categories on pages.
    Is there a code that works also on pages?
    I mean to exclude on the Homepage, but not on another page?

    Kind Regards
    Alex

    1. Hi Alexander,

      Sorry for the late response.

      You can use the same concept code to use it in pages. For example instead of

      if ( $query->is_home() )

      you can use something like

      if ($query->is_page(3) )

      Where 3 is the page ID. Of course then you have to use different filters for when the page loads up the content.

      I hope the above answers your queries. Should you have any further queries, just get in touch with us via our contact form.

        1. What do you mean there is no page ID? Every object in WordPress has an ID. Drop us an email or contact us from our contact form about this. We will help you sort it out 🙂

  6. Unfortunately after implementing this code in my functions.php, the category still shows in post page. Please any solution? I want the category to disappear from my blog, post page, widget or anywhere I decided to display all categories. Waiting for your reply.

    1. Hi Toke,

      Thank you for following our plugin. Unfortunately offhand we cannot really tell what is working or not, since such functionality depends a lot on the theme. If you would like us to assist you further, please send us an email on support@melapress.com.

      Looking forward to hearing from you.

      1. Let’s assume the id of the blog category is 1. Can I add php code about showing all ids greater than -2 so that it doesn’t show all categories greater than 1. Can there be a method that will work with php code that will work dynamically instead of adding an id every time?

        1. Indeed Metin, it is possible to achieve what you want with a bit of PHP code. If you are not familiar with coding, you can always find a developer to help you with it. All the best with building this.

  7. Hi Robert,
    What should I use instead of “is_home” if I want to hide the categories from the category widget that I place on my sidebar?


Leave a Reply

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

Stay in the loop

Subscribe to the Melapress newsletter and receive curated WordPress management and security tips and content.

Newsletter icon

It’s free and you can unsubscribe whenever you want. Check our blog for a taste.

Envelope icon
newsletter-pop-up