0

How I can know if the child categories of current categories are empty ?

I am using below code to filter out categories having no products and having no categories.

 $re_child_terms = get_term_children( $term->term_id, 'category' );
        if ( $term->count > 0 || ! empty( $re_child_terms ) ) {

now want to filter out empty children categories here, is it possible ? how ?

1 Answer 1

1

You're going to need to loop over the $re_child_terms array. According the docs get_term_children returns an array of term_ids so you will need to loop over the ids, get the terms and save them into a new array if they're not empty.

$re_child_term_ids = get_term_children( $term->term_id, 'category' );
$re_child_terms = array();

foreach ( $re_child_terms_ids as $child_id ) {
    $child = get_term($child_id);
    if ( is_object($child) && !($child instanceof WP_Error) ) {
        if ( $child->count > 0 ) {
            $re_child_terms[] = $child;
        }
    }
}

if ( $term->count > 0 || !empty($re_child_terms) ) {
    // do stuff
}

// note: this code is untested but should work

EDIT (encapsulated into a function):

// in functions.php
if ( ! function_exists ) {
    function get_nonempty_term_children( $term_id, $taxonomy ) {
        $child_ids = get_term_children( $term_id, $taxonomy );
        $child_objs = array();

        foreach ($child_ids as $id) {
            $child = get_term($id);
            if ( is_object($child) && !($child instanceof WP_Error) ) {
                if ( $child->count > 0 ) {
                    $child_objs[] = $child;
                }
            }
         }

         return $child_objs;
     }
}

// in template
$re_child_terms = get_nonempty_term_children($term->term_id, 'category');

if ( $term->count > 0 || !empty($re_child_terms) ) {
    // do stuff
}
4
  • I am already inside few loops as you can see using same condition that you have recommended, but would like to know if there is any better way . Commented Oct 17, 2016 at 14:07
  • You are using the same condition as in my example but your original post is missing the foreach loop before it which is the critical point of my example. What exactly do you mean by a 'better' way? more succinct?computationally faster?etc To save yourself some time and effort and make you code more succinct you could encapsulate the functionality into a function Commented Oct 18, 2016 at 15:55
  • I have left those foreach loops. do not want another loop. Commented Oct 18, 2016 at 16:41
  • get_term_children() will return an empty array if no terms are found. So it's not clear why you need anything more. Maybe if you added the part where the condition of childlessness was coming into play, it would be more clear or answerable.
    – CK MacLeod
    Commented Oct 18, 2016 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.