Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;

                }
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted
$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 ); 
share|improve this answer
    
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 Aug 8 '12 at 8:12
    
$args['child_of'] = $term->term_id; will this get the childs of the terms? Seems kinda of wierd? –  Bram Aug 8 '12 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 Aug 8 '12 at 9:24
    
@Bram Have updated my answer, have tested this and this should work now... –  Rajeev Vyas Aug 8 '12 at 10:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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