Search code examples
wordpressfunctioncategoriesarchive

Check if the current post is within a parent category or it's subcategories


I have over 200 categories and I need to be able to check whether the current category (on category.php) is within an array of parent categories.

I've read tons of ways to check if POSTS are IN a category or subcategory, or if ONE cat is a CHILD or PARENT of another. But I cannot find any WP functions or questions regarding checking if current cat is within an array of parent categories.

Here is what I'm trying to accomplish:

  • Parent 1 (ID 1)
    • child 1 (ID 5)
    • child 2 (ID 6)
    • child 3 (ID 7)
      • grandchild 1 (ID 8)
  • Parent 2 (ID 2)
  • Parent 3 (ID 3)
  • Parent 4 (ID 4)

I need to do something like this:

IF( cat_is_child_of( array(1,2,3,4)) ):
    Do something amazing
ELSE
    The current category is NOT within any of the parent categories for the ID's given
ENDIF

Any ideas???

        <?php if (
                is_post_type_archive( 'inventory' ) 
                || get_post_type( get_the_ID() ) == 'inventory' 
                  // check if category IS one of these parent cats
                || is_category(array( 25,28,124,297,298,299 )) 
                  // ugly version of checking if this cat is SUB of any parent cats
                || cat_is_ancestor_of(25, get_query_var( 'cat' ))
                || cat_is_ancestor_of(124, get_query_var( 'cat' ))
                || cat_is_ancestor_of(297, get_query_var( 'cat' ))
                || cat_is_ancestor_of(298, get_query_var( 'cat' ))
                || cat_is_ancestor_of(299, get_query_var( 'cat' ))
            ):
            ?>


Solution

  • After a ton of searching and only coming up with functions that checked whether a category has subcategories, of is a post is in (just) the subcategory, I came across an old post with a function that works:

    if ( ! function_exists( 'post_is_in_a_subcategory' ) ) {
    function post_is_in_a_subcategory( $categories, $_post = null ) {
        foreach ( (array) $categories as $category ) {
            // get_term_children() only accepts integer ID
            $subcats = get_term_children( (int) $category, 'category' );
            if ( $subcats && in_category( $subcats, $_post ) )
                return true;
        }
        return false;
      }
    }
    

    and how I'm using it:

            <?php if (
                is_post_type_archive( 'inventory' ) 
                || get_post_type( get_the_ID() ) == 'inventory' 
                // in one of the parent cats?
                || is_category(array( 25,28,124,297,298,299 )) 
                // in ANY of the subcats?
                || post_is_in_a_subcategory( array( 25,28,124,297,298,299 ) )
                
            ):
            ?>
    

    The original function was in the Wordpress codex, as an example, that was NOT in the core. http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category it has since been removed!