How to Filter WordPress Categories from the_category()

How to Filter WordPress Categories from the_category() featured image

Reading Time: 2 min, 1 sec

On this blog, I have shared various WordPress tips and hacks in past. If you are new to WordPress, I recommend you to read the collection of WordPress tutorials. This post is an addition to a new WordPress topic in that collection. This is another useful post if you are looking to learn WordPress and its development.

There could be various reasons for which you need to print post categories. The most common use is to show all the categories of a post in the blog post. In WordPress, the_category() function is used to display all categories of a specific post. It automatically prints all categories associated with a post. You can also select a separator for printed categories by passing in the function. If you want a comma separated, use the code added below:

Best WordPress Hosting

Bluehost is one of the best and secure WordPress hosting provider
<?php the_category(', ') ?>

But sometimes, you do not want to display all posts. A Few days back, I had the same requirement when I want to hide a specific category from posts. There is no rocket science in this and I achieved that by adding a filter to the_category() function. I found this useful code on pcunleashed.com. This code works fine and you can filter one or more categories. See in the code, it added a filter for categories ‘Mac’ and ‘Windows’. You can change it as per your own requirement.

Also see: WordPress.com vs WordPress.org

Add this code to the functions.php of your theme or create a site-wide plugin to make this code universal. To edit the functions.php, you can either use cPanel’s File Manager or access the files using FTP.

function the_category_filter($thelist,$separator=' ') {
	if(!defined('WP_ADMIN')) {
		//list the category names to exclude
		$exclude = array('Mac','Windows');
		$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);
	} else
		return $thelist;
}
add_filter('the_category','the_category_filter',10,2);

If you have a similar kind of need, you can use this code on your website. You can add filters for as many WordPress categories as you want.

Try this code and see if it works fine for you. I recommend you create a site-wide plugin. By doing this, you will make this code universal. If you edit the functions.php, it will be theme dependent and you will have to put that again if you decide to change the theme later.

Deepanker profile image

Written by Deepanker

Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.

Leave a Reply

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

Need a Hand with Your WordPress Site?

Don't let technical issues slow you down. Our professional WordPress maintenance service handles updates, security, and performance optimization so you can focus on what you do best: creating content.

Learn More About Our Services