How to hide tags or categories from your site

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;
}

Related Articles

Leave a Reply

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