按属性过滤的WooCommerce相关产品

4
我已经查看了这个问题这个问题,但我仍然无法解决。

我有一个名为"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 );

第一版导致没有相关的产品显示,因此它破坏了代码。第二版没有影响。
我该如何解决这个问题?
谢谢

嗯,我记得在这里写过回复……我测试了代码,但没有结果,即使我删除了“post__in”=> $related参数。我想我只能继续尝试了。 - schatzkin
1个回答

1

好的,我有答案了!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'
              )
        )
) );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接