retrieve thumbnail from post ID of best selling product within product_tag taxonomy

Question

I have used the below code to retrieve the thumbnail of best selling product within a product category and thought I could do the same for product tags by simply switching out the taxonomy to product_tag, however it has not worked.

$max_tag_count = 14; // this is 'x'
$qty_per_column = 7; // this is 'y'

$args = array(
    'taxonomy'   => 'product_tag',
    'number'     => $max_tag_count + 1, 
    'hide_empty' => 0,
    'parent'    => 0,
    'orderby'    => 'name',
    'order'      => 'ASC',
);

$get_tags = get_terms( $args );
$get_tags = ( ! is_wp_error( $get_tags ) ) ? $get_tags : [];

$total = count( $get_tags );
$list_number = 1;
$_new_col = false;

$columns = '';

$loop1 = new WP_Query( [
            'post_type'      => 'product',
            'posts_per_page' => 1,
            'meta_key'       => 'total_sales',
            'orderby'        => 'meta_value_num',
        ] );
            while ( $loop1->have_posts() ) {
                $loop1->the_post();
                if ( has_post_thumbnail() ) {
                    $default_thumb_id = get_post_thumbnail_id();
                    $default_thumb = $default_thumb_id ? wp_get_attachment_url( $default_thumb_id ) : '';
                }
                else {
                    $default_thumb = content_url( '/uploads/woocommerce-placeholder-416x416.png' );
                }
            }
        wp_reset_postdata();


foreach ( $get_tags as $i => $tag ) {
    if ( $i >= $max_tag_count ) {
        break;
    }

    if ( $i % $qty_per_column === 0 ) {
        if ( $_new_col ) {
            $columns .= '</ul></div><!-- .tag-col -->';
        }

        $columns .= '<div class="tag-col" >';

        $_new_col = true;
        $list_number++; 
    }

    if ( $total > $max_tag_count && $i === $max_tag_count - 1 ) {
        $columns .= '<li class="all-link"><a href="/view-all">View All</a></li>'; 
    } 
    else {
        
        $loop2 = new WP_Query( [
            'post_type'      => 'product',
            'posts_per_page' => 1,
            'meta_key'       => 'total_sales',
            'orderby'        => 'meta_value_num',
            'tax_query'      => array(
                array(
                    'taxonomy' => 'product_tag',
                    'field'    => 'slug',
                    'terms'    => $tag->slug,
                ),
            ),
        ] );
        if ( $loop2->have_posts() ) {
            while ( $loop2->have_posts() ) {
                $loop2->the_post();
                if ( has_post_thumbnail() ) {
                    $thumbnail_id = get_post_thumbnail_id();    
                    $image = $thumbnail_id ? wp_get_attachment_url( $thumbnail_id ) : '';
                }
                else {
                    $image = content_url( '/uploads/woocommerce-placeholder-416x416.png' );
                }
            }
        }
        else {
            $image = null;
        }
        wp_reset_postdata();
    
        
        $link = '<a href="' . esc_url( get_term_link( $tag ) ) . '">' . esc_html( $tag->name ) . '</a>';
        $columns .= '<li class="menu-item" data-image="' . esc_url( $image ) . '">' . $link . '</li>';
            
    } 
} 


if ( $_new_col ) {
    $columns .= '</ul></div><!-- .tag-col -->';
}
0
, , , , aye cee 4 months 0 Answers 44 views 0

Leave an answer