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.
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.
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.
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.
8 responses to “How to hide tags or categories from your site”
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
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.
Thanks for this! Can the code be modified to hide multiple tags or categories?
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
toin_array( $term->slug, $term_to_unset, true )
That should do it.
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?
You are missing a closing parenthesis right before the opening curly bracket of the second if statement. i.e.:
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
Hello Alain.
Things are a bit different with WooCommerce, are you trying to hide a certain tag from a certain place?