0

I'm modding a theme to use parent taxonomy terms instead of all taxonomy terms, now I've been able to only display the parents, but now I've run into a count problem.

Basicly this is the code that has to be changed

$_products_in_term = get_objects_in_term( $term->term_id, $taxonomy ); 

This gets all the terms of the current term, which then later is used to do an intercept with and then count. Now how could I mod this code to not only get the terms of the current term but if it has children get the terms of all its children , (will only go 1 deep, not gonna work with grandchildren)

This is the code the full code

               foreach ($terms as $term) { 

                // Get count based on current view - uses transients
                $transient_name = 'wc_ln_count_' . md5( sanitize_key($taxonomy) . sanitize_key( $term->term_id ) );

                if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) {

                //  $_products_in_term = get_objects_in_term( $term->term_id, $taxonomy );

                $args2['child_of'] = $term->term_id;
                $terms = get_terms( $taxonomy , $args2 );
                $term_array=array();
                foreach($terms as $terms){
                $term_array[]= $terms->term_id;
                }

                $_products_in_term = get_objects_in_term( $term_array, $taxonomy ); 

                    set_transient( $transient_name, $_products_in_term );
                }

                $option_is_set = (isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms'])) ; 

                // If this is an AND query, only show options with count > 0
                if ($query_type=='and') {

                    $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids));

                    // skip the term for the current archive
                    if ( $current_term == $term->term_id ) continue; 

                    if ($count>0 && $current_term !== $term->term_id ) $found = true;

                    if ($count==0 && !$option_is_set) continue;

                // If this is an OR query, show all options so search can be expanded
                } else { 

                    // skip the term for the current archive
                    if ( $current_term == $term->term_id ) continue;

                    $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids));

                    if ($count>0) $found = true;

                }

1 Answer 1

2
$terms = get_term_children( $term->term_id , $taxonomy );
$term_array=array();
foreach($terms as $terms){
  $term_array[]= $terms;
}

$_products_in_term = get_objects_in_term( $term_array, $taxonomy ); 
5
  • Doesnt work, should be very close to solution tho, could you check if you maybe made an error?, added full code and your replacement.
    – Bram
    Commented Aug 8, 2012 at 8:12
  • $args['child_of'] = $term->term_id; will this get the childs of the terms? Seems kinda of wierd?
    – Bram
    Commented Aug 8, 2012 at 8:54
  • OKee tested some more, problem is it gets the id of all the terms not just the terms of the current term and its children. You fill that first args with parents of the term not? 'child_of' seems to give IDs of its parent? Should be giving Id's of its children
    – Bram
    Commented Aug 8, 2012 at 9:24
  • @Bram Have updated my answer, have tested this and this should work now... Commented Aug 8, 2012 at 10:20
  • Current code is functional thanks. Variable names are very confusing though. foreach($terms as $terms) is a crime against semantics, should be foreach($terms as $child_term_id). It would be much clearer like that :)
    – jerclarke
    Commented May 11, 2017 at 17:20

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.