Taxonomy menu with post count and multiple parents

I adjusted your code a little bit to integrate the wp_query() class instead of query posts(), which is only meant for altering the main loop. You should always opt to use wp_query() when trying to create secondary loops.

Since we’re using wp_query(), we’re also going to have to use wp_reset_postdata() instead of wp_reset_query. Im not sure if this is going to fix your problem, but adjust your code to this, and we’ll tackle the rest of your problems step by step.

<?php $auto_types = get_terms('type', 'hide_empty=1'); ?>
<ul>
<?php foreach( $auto_types as $auto_type ) : ?>
    <li>
    <a href="https://wordpress.stackexchange.com/questions/18495/<?php echo get_term_link( $auto_type->slug,"type' ); ?>"> 
        <?php echo $auto_type->name; ?>
    </a>
    <?php $auto_brands = get_terms('brand', 'parent=0' ); ?>
        <ul>
            <?php foreach ($auto_brands as $auto_brand) : ?>
                <?php $brand_filter['tax_query'] = array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'type',
                            'terms' => array($auto_type->slug),
                            'field' => 'slug',
                        ),
                        array(
                            'taxonomy' => 'brand',
                            'terms' => array($auto_brand->slug),
                            'field' => 'slug',
                        ),
                    );
                    $tax_query = new WP_Query($brand_filter);
                    if ( $tax_query->have_posts() ) :

                        $count = 1;
                        while ( $tax_query->have_posts() ) : 
                        $tax_query->the_post();

                        if ( $count >= 1 ) { ?>
                           <li>
                               <a href="/?type=<?php echo $auto_type->slug ?>&brand=<?php echo $auto_brand->slug ?>">
                                   - - <?php echo $auto_brand->name; ?> (<?php echo $count; ?>)
                               </a>
                           </li>
                        <? }

                        $count++;

                        endwhile; 
                        wp_reset_postdata();

                    endif; 
                endforeach 
            ?>
        </ul>                 
    </li>           
<?php endforeach ?>
</ul>

UPDATE: I added the posts_per_page parameter and set it to -1 to show all posts. I tested it on my side. It should give you the results you were looking for.

<?php $auto_types = get_terms('type', 'hide_empty=1'); ?>
<ul>
<?php foreach( $auto_types as $auto_type ) : ?>
    <li>
    <a href="https://wordpress.stackexchange.com/questions/18495/<?php echo get_term_link( $auto_type->slug,"type' ); ?>"> 
        <?php echo $auto_type->name; ?>
    </a>
    <?php $auto_brands = get_terms('brand', 'parent=0' ); ?>
        <ul>
            <?php foreach ($auto_brands as $auto_brand) : ?>
                <?php $brand_filter = array(
                    'posts_per_page' => '-1',
                    'tax_query' => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'type',
                            'field' => 'slug',
                            'terms' => array($auto_type->slug),
                        ),
                        array(
                            'taxonomy' => 'brand',
                            'field' => 'slug',
                            'terms' => array($auto_brand->slug),
                        )
                    )
                );
                $tax_query = new WP_Query($brand_filter);
                    $count = 0;
                if ( $tax_query->have_posts() ) : while ( $tax_query->have_posts() ) : $tax_query->the_post();
                    $count++;
                endwhile; endif; wp_reset_postdata();
                    if ( $count > 0 ) : ?>
                        <li>
                            <a href="/?type=<?php echo $auto_type->slug ?>&brand=<?php echo $auto_brand->slug ?>">
                                - - <?php echo $auto_brand->name; ?> (<?php echo $count; ?>)
                            </a>
                        </li>
            <?php endif; endforeach ?>
        </ul>                 
    </li>           
<?php endforeach ?>
</ul>

Leave a Comment