php – 按属性过滤的WooCommerce相关产品

我看过
this question
this one,但我仍然被卡住了.

我有一个属性“status”,我只想要出现值为“OPEN”的类(产品).我正在编辑related.php WooCommerce模板文件.

这是我尝试过的两个代码版本.

版本1:

 $args = apply_filters( 'woocommerce_related_products_args', array(
'post_type'            => 'product',
'ignore_sticky_posts'  => 1,
'no_found_rows'        => 1,
'posts_per_page'       => $posts_per_page,
'orderby'              => $orderby,
'post__in'             => $related,
'post__not_in'         => array( $product->id ),
'meta_query' => array(
   array(
    'key' => 'status',
    'value' => 'OPEN',
   ),
 ),
 ) );

版本2:

    $key="status";
    $value="OPEN";
    $query_status = array('meta_key' => $key, 'meta_value' => $value);
    $meta_query[] = $query_status;

    $args = apply_filters( 'woocommerce_related_products_args', array(
    'post_type'            => 'product',
    'ignore_sticky_posts'  => 1,
    'no_found_rows'        => 1,
    'posts_per_page'       => $posts_per_page,
    'orderby'              => $orderby,
    'post__in'             => $related,
    'post__not_in'         => array( $product->id ),
    'meta_query'           => $meta_query,
     ) );

    $products                    = new WP_Query( $args );

第一个版本不会显示相关产品,因此会破坏代码.第二个没有效果.

我该如何解决这个问题?

谢谢

最佳答案 好的,我有答案! WooCommerce以两种方式存储自定义属性,但在这种情况下,我需要使用术语查询而不是元查询.这是最终的查询,就像一个魅力:

$args = apply_filters( 'woocommerce_related_products_args', array(
       'post_type'            => 'product',
       'ignore_sticky_posts'  => 1,
       'no_found_rows'        => 1,
       'posts_per_page'       => 4,
       'orderby'              => $orderby,
       'post__not_in'         => array( $product->id ),
       'tax_query'      =>     array(
              array(
                      'taxonomy' => 'pa_status',
                      'field' =>      'slug',
                      'terms' => 'open'
              )
        )
) );
点赞