How to Determine if a post is in a category or its subcategory in WordPress

in Tutorials & Samples/Wordpress

In WordPress, you can use the function in_category() to determine if a particular post is in a given category. The function looks like the following:

<?php in_category( $category, $_post ) ?>

The function will return true or false depending if the given post belongs to the specified category. It’s a pretty useful function for doing WordPress theme development since it allows you to specify the category ID or the category name for the $category parameter. (I personally never include the second parameter $_post since it defaults to the current post in the loop).

So, thanks to this function, you can do neat customizations to your theme like in the example below:

<?php
if ( in_category('fruit') ) {
	include 'single-fruit.php';
} elseif ( in_category('vegetables') ) {
	include 'single-vegetables.php';
} else {
	// Continue with normal Loop
	if ( have_posts() ) : while ( have_posts() ) : the_post();
	// ...
}
?>

However, the in_category() function will not test if the post belongs in a sub_category of the given category. So, if you have a category called fruit, and you have a subcategory in fruit called apple, if you assign a post to apple but not to apple, the in_category function will say that the post does not belong to the category fruit.

To get around this, the developers at WordPress have created a function called post_is_in_descendant_category. This function will test if a given post belongs to any subcategories of a given category. Here is what the function looks like:

<?php
/**
 * Tests if any of a post's assigned categories are descendants of target categories
 *
 * @param int|array $cats The target categories. Integer ID or array of integer IDs
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
 * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
 * @uses get_term_children() Passes $cats
 * @uses in_category() Passes $_post (can be empty)
 * @version 2.7
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
 */
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
	function post_is_in_descendant_category( $cats, $_post = null ) {
		foreach ( (array) $cats as $cat ) {
			// get_term_children() accepts integer ID only
			$descendants = get_term_children( (int) $cat, 'category' );
			if ( $descendants && in_category( $descendants, $_post ) )
				return true;
		}
		return false;
	}
}
?>

Although this function is currently not part of the WordPress core, you can simply copy and paste it into your functions.php file that you use for your theme. (I’ve done so and I use this function in many of my themes)

Now, with this function and the classic in_category() function, you can determine if a post is in a category or its subcategory. Suppose that you have a category named fruit and its category ID is 11, here is how you can determine if a post is in the category or its subcategory:

Determine if a post is in category or its subcategory based on post ID

<?php
 // Post is assigned to "fruit" category or any descendant of "fruit" category?
 if ( in_category( 11 ) || post_is_in_descendant_category( 11 ) ) {
	// These are all fruits…
}
?>

Determine if a post is in category or its subcategory based on category name

<?php
 // Post is assigned to "fruit" category or any descendant of "fruit" category?
 // we need to get the category name and then get the category ID from the name
 $category_name = 'fruit';
 $category_to_check = get_term_by( 'name', $category_name, 'category' );
 if ( in_category( $category_name ) || post_is_in_descendant_category( $category_to_check->term_id ) ) {
	// These are all fruits…
}
?>

Please note that I did not create the function post_is_in_descendant_category(), it was created by the WordPress team. I’m just explaining how to properly use it in your WordPress theme.

Tags:

Mifty Yusuf is a Montreal-based software developer who enjoys playing with new web technologies as well as comic books and illustrations. He beleives that, no matter what the question is, the answer is always Batman!

3 Comments

  1. Hi Mifty,
    I just found this post while looking for a solution very similar to this. The only difference being that I would need it to work with WooCommerce categories. I assume I would have to make some adjustments (?), but I just can’t get it to work.
    Any chance you have an idea?
    Thanks a lot!
    – Jenny

Leave a Reply

Your email address will not be published.

*

Latest from Tutorials & Samples

Go to Top