php – 按属性过滤的WooCommerce相关产品
发布时间:2020-12-13 16:05:16 所属栏目:PHP教程 来源:网络整理
导读:我看过 this question和 this one,但我仍然被卡住了. 我有一个属性“status”,我只想要出现值为“OPEN”的类(产品).我正在编辑related.php WooCommerce模板文件. 这是我尝试过的两个代码版本. 版本1: $args = apply_filters( 'woocommerce_related_products
我看过
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','meta_query' => $meta_query,) ); $products = new WP_Query( $args ); 第一个版本不会显示相关产品,因此会破坏代码.第二个没有效果. 我该如何解决这个问题? 谢谢 解决方法
好的,我有答案! WooCommerce以两种方式存储自定义属性,但在这种情况下,我需要使用术语查询而不是元查询.这是最终的查询,就像一个魅力:
$args = apply_filters( 'woocommerce_related_products_args',array( 'post_type' => 'product','posts_per_page' => 4,'tax_query' => array( array( 'taxonomy' => 'pa_status','field' => 'slug','terms' => 'open' ) ) ) ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |