Hiding taxonomies with no children WP_Query & tax_query

Question

I have this code which has taken over 7 hours to put together through research but the last hours or so I have hit a brick wall!

I require the taxonomy title to be hidden if there are no posts associated with it.

Here is the page: http://cb.dannycheeseman.me/wine-menu/usa/california/

Here is my code:

/**********************************************/
//  CUSTOM WINE MENU SHORTCODE
/**********************************************/


add_shortcode( 'wine_list_per_cat', 'wine_list_per_cat' );

function wine_list_per_cat($atts){

    global $woocommerce_loop;

    extract(shortcode_atts(array(
        'cat' => '',    // list of categories in the format 0,1,2,3,4
        'tax' => 'product_cat', // taxonomy use
        'per_cat' => '3',   // max featured items to display per category
        'columns' => '3',   // columns size
    ), $atts));
?>

<ul id="wine-menu">

    <?php  

        $wine_type_terms = get_terms( 'wine-type' );

        foreach ( $wine_type_terms as $wine_type_term ) {

            $args = array(
                'post_type' => 'product',
                'showposts' => -1,
                'tax_query' => array(
                    'relation' => 'IN',
                    array(
                        'taxonomy' => 'wine-type',
                            'field' => 'slug',
                            'terms' => array( $wine_type_term->slug ),
                        ),
                        array(
                            'taxonomy' => 'product_cat',
                            'field' => 'slug',
                            'terms' => $cat
                        )
                    ));

            $wine_type_query = new WP_Query($args);?>
            <li>
            <h2><?php echo $wine_type_term->name; ?></h2>
            <ul>
                <?php  while ( $wine_type_query->have_posts() ) : $wine_type_query->the_post();global $product ?>
                    <li class="wine-item"> 
                        <h3><?php the_title(); ?></h3>
                        <span class="wine-bottle-size">Size: <?php echo get_post_meta( get_the_ID(),'wine_bottle_size', true );?></span><br/>
                        <span class="wine-percentage">Percentage: <?php echo get_post_meta( get_the_ID(), 'wine_percentage', true );?></span><br/>
                        <span class="wine-year">Year: <?php echo get_post_meta( get_the_ID(), 'wine_year', true );?></span><br/>
                        <span class="wine-price">Price: <?php echo $product->get_price_html(); ?></span><br/>
                        <div class="wine-content"><?php echo get_post_meta( get_the_ID(), 'wine_menu_content', true );?></div>
                        <?php woocommerce_template_loop_add_to_cart( $wine_type_query->post, $product ); ?>
                    </li>


                <?php endwhile;?>
                <?php wp_reset_postdata(); ?>
            </ul>


         </li>               
        <?php } ?>

        <?php wp_reset_query(); ?>

</ul><!--/.products-->
<?php } 

UPDATE!
OK!!! Woooohooo!
Found out what it was.. I should have been calling if have posts before the call for $tax->name;
Here is my final, and working code! 🙂

/**********************************************/
//  CUSTOM WINE MENU SHORTCODE
/**********************************************/


add_shortcode( 'wine_list_per_cat', 'wine_list_per_cat' );

function wine_list_per_cat($atts){

    global $woocommerce_loop;

    extract(shortcode_atts(array(
        'cat' => '',    // list of categories in the format 0,1,2,3,4
        'tax' => 'product_cat', // taxonomy use
        'per_cat' => '3',   // max featured items to display per category
        'columns' => '3',   // columns size
    ), $atts));
?>

<ul id="wine-menu">

    <?php  

        $taxonomy = get_terms( 'wine-type', array('hide_empty' => true, 'pad_counts' => true));

        foreach ( $taxonomy as $tax ) {

            $args = array(
                'post_type' => 'product',
                'orderby' => 'title',
                'showposts' => -1,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' => $cat,
                        'operator' => 'IN'
                    ),
                    array(
                        'taxonomy' => 'wine-type',
                        'field' => 'slug',
                        'terms' => array( $tax->slug ),
                        'operator' => 'IN'
                    ),

                ));

            $wine_type_query = null;

            $wine_type_query = new WP_Query($args);

            if( $wine_type_query->have_posts() ) : ?>

                <li id="wine-type">
                    <h2><?php echo $tax->name; ?></h2>

                    <ul>
                        <?php while ( $wine_type_query->have_posts() ) : $wine_type_query->the_post();global $product ?>
                        <li class="wine-item"> 
                            <h3><?php the_title(); ?></h3>
                            <span class="wine-bottle-size">Size: <?php echo get_post_meta( get_the_ID(),'wine_bottle_size', true );?></span><br/>
                            <span class="wine-percentage">Percentage: <?php echo get_post_meta( get_the_ID(), 'wine_percentage', true );?></span><br/>
                            <span class="wine-year">Year: <?php echo get_post_meta( get_the_ID(), 'wine_year', true );?></span><br/>
                            <span class="wine-price">Price: <?php echo $product->get_price_html(); ?></span><br/>
                            <div class="wine-content"><?php echo get_post_meta( get_the_ID(), 'wine_menu_content', true );?></div>
                            <?php woocommerce_template_loop_add_to_cart( $wine_type_query->post, $product ); ?>
                        </li>
                        <?php endwhile;?>

                    </ul>

                </li> 
            <?php endif; wp_reset_query(); ?>

    <?php } ?>

</ul><!--/.products-->
<?php } 
0
, , , , user49899 7 years 2014-04-03T19:14:52-05:00 0 Answers 101 views 0

Leave an answer

Browse
Browse