admin管理员组

文章数量:1315225

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–>

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–>
Share Improve this question asked Nov 19, 2020 at 0:32 Marney FontanaMarney Fontana 31 bronze badge 1
  • You need to use a tax_query. developer.wordpress/reference/classes/wp_query/… – shanebp Commented Nov 19, 2020 at 2:34
Add a comment  | 

1 Answer 1

Reset to default 0

So as @shanebp suggested you have to use tax_query it would also help if you use operator in tax_query. following is your code after make some minor changes;

<ul class="products-newo">
    <?php
    $attributes = $product->get_attributes();
    $pa_brand = $attributes["pa_brand"];
        
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 4,
        'tax_query'      => array( array(
            'taxonomy'        => 'pa_brand',
            'field'           => 'slug',
            'terms'           =>  $pa_brand->get_slugs(),
            'operator'        => 'IN',
        ) )
        );
    $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>

But i also want to suggest if you check a small condition just after the $attributes = $product->get_attributes(); line. if( isset($attributes["pa_brand"]) ) Otherwise if any product does not have this value you will get error. Thank you.

本文标签: woocommerce offtopicReturn product list based on current product attribute