我已经按类别过滤了,但不知道如何按自定义属性或元值过滤产品
代码:
$key="naam"; //custom attribute name
$value="test";// custom value
$query_custom = array('key' => $key, 'value' => $value);
$meta_query[] = $query_custom ;
$args=array('meta_query'=>$meta_query, 'product_cat' => 'activiteiten','posts_per_page' => 10,'post_type' => 'product');
$loop = new WP_Query( $args );
`
最佳答案
这是一个查询的工作示例,涉及:
- 产品类别过滤
- 发布元值过滤(元查询)
- 商品属性值过滤(税务查询)
代码:
$products = new WP_Query( array(
'posts_per_page' => 10,
'post_type' => 'product',
'post_status' => 'publish',
// 1. Product category filter
'product_cat' => 'clothing',
// 2. The Post meta query part (filtering by post meta value)
'meta_query' => array( array(
'key' => '_price',
'value' => 5,
'type' => 'numeric',
'compare' => '>',
), ),
// 3. The taxonomy meta query part (filtering by term values)
'tax_query' => array( array(
'taxonomy' => 'pa_color', // Product attribute taxonomy: always start with 'pa_'
'field' => 'slug', // Can be 'term_id', 'slug' or 'name'
'terms' => array('blue'),
), ),
) );
// Testing output
if( $products->have_posts() ) :
echo '<ul>'
while ( $products->have_posts() ) : $products->the_post();
echo '<li class="post-id-' . get_the_id() . '">' . get_the_title() . '</li>';
endwhile;
wp_reset_postdata();
echo '</ul>'
endif;
测试和工作:
Wordpress WP_Query
的官方引用文档:
关于php - 按自定义产品属性过滤产品并在 WP_Query 中发布元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54004962/