Use the coupon code WORDPRESS and save 30% OFF! Buy Now

How to hide tags or categories from your site

Posted On
How to hide tags or categories from your site WordPress template

Sometimes you might have a category or tag that you want to prevent from displaying on category list widgets, tag clouds, the post’s meta, or anywhere else for that matter. For example, in our popular blogging theme Public Opinion we use the tag featured to denote the posts that will appear on the theme’s featured section. We did not want to display this tag on places tags are listed since it’s there just to help you manage your featured post with ease and not provide any info to the site’s visitor. Today we’ll create a small plugin to help us hide a category or tag on any theme.

The plugin

Let’s start off by creating a new plugin folder under the /wp-content/plugins/ folder of our installation. In our example we will call this folder ci-hide-cat-tag, once the folder is created go ahead and enter it and create a new file named ci-hide-cat-tag.php.

In the new file paste in the plugin’s header.

<?php
/*
Plugin Name: Hide categories and tags
Plugin URI:
Description: This plugin hides categories and/or tags
Version: 1.0
Author: cssigniterteam
Author URI: https://cssigniter.com/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
Plugin header

To achieve the desired functionality we are going to use two filters one on the get_terms() function, which returns lists of terms attached to a particular taxonomy, for example it’s used to generate the tag cloud widget, and another one on get_the_terms() which returns the terms attached to a post, for example get_the_terms shows us which tags is a single post filed under.

Let’s add our filters now, in our example we will be removing the audio term from the post_tag (tags) taxonomy. We’ll start with the filter for get_terms. Paste this code below the plugin’s header.

// Hide the tag or category of your choosing.
add_filter( 'get_terms', 'ci_get_terms_hide_term', 10, 3 );
function ci_get_terms_hide_term( $terms, $taxonomies, $args ) {
if ( is_admin() ) {
return $terms;
}

if ( ! in_array( 'post_tag', $taxonomies, true ) ) {
return $terms;
}

if ( empty( $terms ) ) {
return $terms;
}

if ( 'all' !== $args['fields'] ) {
return $terms;
}

$term_to_unset = 'audio';

if ( $term_to_unset ) {
foreach ( $terms as $key => $term ) {
if ( is_object( $term ) && $term_to_unset === $term->slug ) {
unset( $terms[ $key ] );
}
}
}

return $terms;
}

In order to get desirable results we start with some checks. First, we check if we are viewing the admin dashboard, in that case we don’t filter anything, next we check if post_tag is a valid taxonomy, if it’s not we can’t filter it, so we stop here. The next check makes sure there are actually terms to filter, if not, we stop here. Finally we check if we will be getting complete term objects for our filters to work properly. If our checks are passed successfully we then let our function know which term we want it to filter out by assigning it to the $term_to_unset variable, in our example audio, finally we loop through all term objects and unset theĀ audio one.

On to the next filter now for get_the_terms. Go ahead and paste this code in.

add_filter( 'get_the_terms', 'ci_get_the_terms_hide_term', 10, 3 );
function ci_get_the_terms_hide_term( $terms, $id, $taxonomy ) {
if ( is_admin() ) {
return $terms;
}

if ( 'post_tag' !== $taxonomy ) {
return $terms;
}

if ( empty( $terms ) ) {
return $terms;
}

$term_to_unset = 'audio';

if ( $term_to_unset ) {
foreach ( $terms as $key => $term ) {
if ( $term_to_unset === $term->slug ) {
unset( $terms[ $key ] );
}
}
}

return $terms;
}

We start again by making sure we’ll get the results we expect. There are three main checks here, the first and third are the same as in the previous filter, the second one makes sure that we are targeting the correct taxonomy for the single post, in our example post_tag. The rest of the code is similar in functionality, we run through the post’s available terms and unset our target one.

That’s it

Now the audio post tag will not be shown on tag lists, whether it is the tag cloud, the single post’s tag list or anywhere else. However if you want to display it on the menu you can use its archive URL as usual e.g. yoursite.com/tag/audio. In the same manner you can hide category terms, just replace the references to post_tag with category and the audio tag slug with the slug of the category you want to hide. We hope you found this small tutorial useful. If you have any questions please let us know in the comments below.

The complete plugin file

<?php
/*
Plugin Name: Hide categories and tags
Plugin URI:
Description: This plugin hides categories and/or tags
Version: 1.0
Author: cssigniterteam
Author URI: https://cssigniter.com/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

// Hide the tag or category of your choosing.
add_filter( 'get_terms', 'ci_get_terms_hide_term', 10, 3 );
add_filter( 'get_the_terms', 'ci_get_the_terms_hide_term', 10, 3 );

function ci_get_terms_hide_term( $terms, $taxonomies, $args ) {
if ( is_admin() ) {
return $terms;
}

if ( ! in_array( 'post_tag', $taxonomies, true ) ) {
return $terms;
}

if ( empty( $terms ) ) {
return $terms;
}

if ( 'all' !== $args['fields'] ) {
return $terms;
}

$term_to_unset = 'audio';

if ( $term_to_unset ) {
foreach ( $terms as $key => $term ) {
if ( is_object( $term ) && $term_to_unset === $term->slug ) {
unset( $terms[ $key ] );
}
}
}

return $terms;
}

function ci_get_the_terms_hide_term( $terms, $id, $taxonomy ) {
if ( is_admin() ) {
return $terms;
}

if ( 'post_tag' !== $taxonomy ) {
return $terms;
}

if ( empty( $terms ) ) {
return $terms;
}

$term_to_unset = 'audio';

if ( $term_to_unset ) {
foreach ( $terms as $key => $term ) {
if ( $term_to_unset === $term->slug ) {
unset( $terms[ $key ] );
}
}
}

return $terms;
}

8 responses to “How to hide tags or categories from your site”

  1. Sam says:

    Hello !
    Thank you for this simple guide.
    But it doesn’t work for me… I need to hide a category from the categories archive page.
    Thank you,
    Sam

    • Nik says:

      Hello Sam.
      On lines 8 & 7 respectively on our code examples you will need to change ‘post_tag’ to ‘category’ and then in lines 20 & 15 respectively replace ‘audio’ with the slug of the category you want to hide.

  2. Joe Bloggs says:

    Thanks for this! Can the code be modified to hide multiple tags or categories?

    • Nik says:

      Hello.
      You can replace the $term_to_unset = 'audio'; bit with an array of the terms you want to hide, for example $term_to_unset = ['audio', 'video']; and then modify this bit $term_to_unset === $term->slug to in_array( $term->slug, $term_to_unset, true )
      That should do it.

      • James says:

        Thank you for the example here. I too would like to use the array. However following your edits to the original I get the following error: Parse error: syntax error, unexpected ‘unset’ (T_UNSET) in /public_html/wp-content/plugins/ci-hide-cat-tag/ci-hide-cat-tag.php on line 39

        I have the array looking like this on the first part:

        $term_to_unset = [‘featured’,’featured_parenting’,’featured_competition’];

        if ( $term_to_unset ) {
        foreach ( $terms as $key => $term ) {
        if ( is_object( $term ) && in_array( $term->slug, $term_to_unset, true ) {
        unset( $terms[ $key ] );
        }
        }
        }

        Any ideas on this one?

        • Anastis Sourgoutsidis says:

          You are missing a closing parenthesis right before the opening curly bracket of the second if statement. i.e.:

          if ( is_object( $term ) && in_array( $term->slug, $term_to_unset, true ) ) {
  3. Alain says:

    Yeah unfortunately that doesn’t work for Woocommerce product tags.

    I tried replacing
    if ( ! in_array( ‘post_tag’, $taxonomies, true ) ) {
    by
    if ( ! in_array( ‘product_tag’, $taxonomies, true ) ) {
    in both parts of the code, without success.

    SAD

Leave a Reply

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

Get access to all WordPress themes & plugins

24/7 Support Included. Join 115,000+ satisfied customers.

Pricing & Sign Up

30-day money-back guarantee. Not satisfied? Your money back, no questions asked.

Back to top