How to Exclude Specific Categories from the Meta Info in WordPress

Bluehost
How to Exclude Specific Categories from the Meta Info in WordPress

Almost all WordPress posts have at least one category. They help you section off your website and keep things neat and tidy – not to mention providing signals that allow us to created related links and all of that other good stuff. If you have one or multiple categories assigned to a post, most WordPress themes will display them alongside your post in one way or another. For example, the TwentySixteen theme shows the meta information (including the categories) on the left hand side of the post like this:

However, sometimes you simply don’t want to assign a category to a post. Maybe your blog is only about one thing, and there’s no need to slot it under a category. In such situations, WordPress makes use of the default “Uncategorized” category. You don’t need to specifically select it. If you don’t deliberately choose a category for your post, it will automatically become “Uncategorized”.

For example in the above post if I remove the “Technology” category, here’s what I see instead:

This looks a bit weird and a bit unprofessional. It almost feels as if you haven’t taken the trouble to write the post properly! Of course it might be completely useless to assign a category for your needs, but your audience doesn’t know that!

Why Exclude Specific Categories?

Scenario 2:

Let’s take another scenario. Sometimes we assign categories to posts or pages for internal management purposes. Let’s say for example, that you want to display certain posts with a different background color, or a different type of font. To do this, you create a special category called “Formatted”. And you code your site so that all posts that fall into the “Formatted” category, are styled differently.

In such a situation, you don’t want the “Formatted” category to be visible to your users. Of course, it might so happen that all “Formatted” posts have a similar theme, but they may not. In any case, it makes no sense to someone who doesn’t understand the purpose and isn’t user friendly. So while we want all the other categories to show up in the list of categories to which the post belongs, we want to exclude “Formatted”. And perhaps a few others as well.

Scenario 3:

On my personal blog, I have a category called “NR”. All posts with this category are excluded from showing up on the home page of my blog, for one reason or another, via some code in functions.php. However, I don’t want this category to show up on to visitors because it means nothing to them. For this reason, we need a way to exclude a single or multiple categories from showing up.

Code to Prevent the Display of Certain Categories

In your functions.php file, or wherever you place your custom PHP code, add the following:

function exclude_these_categories($thelist, $separator=' ') {
        //Exclude the following categories
        $exclude = array('Uncategorized');
        $cats = explode($separator, $thelist);
        $newlist = array();
        foreach($cats as $cat) {
            $catname = trim(strip_tags($cat));
            if(!in_array($catname, $exclude))
                $newlist[] = $cat;
        }
        return implode($separator, $newlist);

}
add_filter('the_category','exclude_these_categories', 10, 2);

This code filters the results of the function “the_category”. In line three, we just need to provide a comma separated list of category names that we want hidden from display. In this, I have just one – “Uncategorized”. But it can include however many names you want. After saving this code, the “Uncategorized” category will no longer show up in the list of category names as shown here:

You can even go one step further and exclude these categories only for certain pages like “single”. To do that, you just need to enclose the above code within an “if” statement like this:

if ( is_single() ) {
// Content goes here
}

This way, you’ll only affect the display on individual pages and not others that might require the “the_category” function to operate properly.

2 Comments on “How to Exclude Specific Categories from the Meta Info in WordPress”!

  1. I tried this code in my child them function.php but it did not remove the category “all posts”

    function exclude_these_categories($thelist, $separator=’ ‘) {
    //Exclude the following categories
    $exclude = array(‘all_posts’);
    $cats = explode($separator, $thelist);
    $newlist = array();
    foreach($cats as $cat) {
    $catname = trim(strip_tags($cat));
    if(!in_array($catname, $exclude))
    $newlist[] = $cat;
    }
    return implode($separator, $newlist);

    }
    add_filter(‘the_category’,’exclude_these_categories’, 10, 2);

  2. I’m using this code and it works, but it also makes the categories in the post edit page to be invisible. The checkboxes are there, but the category names next to it is gone.

Leave a Reply

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

Disclosure: We receive a compensation from some of the companies whose products are presented on our website.