Return product list based on current product attribute

Question

I have modified my single-product.php and want to pop in a block of other products that share the same brand name which I have specified in a product attribute called ‘brand’.

This is my code so far but it is returning all the products and NOT filtering by the pa_brand.

<!-- Custom 4 up product filtered by Brand attribute -->    
    <ul class="products">
                <?php
            $args = array(
                'post_type' => 'product',
                'posts_per_page' => 4,
                'taxonomy' => 'pa_brand',
                'field'    => 'slug',
                'terms'    => $product->get_attribute( 'brand' )
                );
            $loop = new WP_Query( $args );
            if ( $loop->have_posts() ) {
                while ( $loop->have_posts() ) : $loop->the_post();
                    wc_get_template_part( 'content', 'product' );
                endwhile;
            } else {
                echo __( 'No products found' );
            }
            wp_reset_postdata();
        ?>
    </ul><!–/.products–>
0
Marney Fontana 2 months 0 Answers 12 views 0

Leave an answer