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