我试图根据表单输入字段创建一个简单的表单来查询woocommerce产品葡萄酒,如下所示:
按类别和价格进行过滤是有效的,但是标签给出了混合的结果,我不知道为什么。
我的表单就是这样给出一些上下文的:

这是我的代码:
$custom_query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'order' => 'DESC',
'posts_per_page' => 3,
'product_tag' => array($tag1, $tag2, tag3),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category ) ),
'field' => 'slug',
'operator' => 'OR'
)),
//Price
'meta_query' => array(
array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
)
);在输入字段中,我有3个产品标签变量(如$tag1、$tag2、$tag3)、1个产品类别变量(如$category)和2个价格范围变量(如$clow和$chigh),这是to的价格。
有人知道为什么会发生这种事吗?
发布于 2019-02-15 23:34:44
代码中有一些小错误:
$在tag3里不见了,OR不是一个operator值(也不需要它),因此,尝试以下修改后的代码:
$custom_query_args = array(
'posts_per_page' => 3,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'order' => 'DESC',
'tax_query' => array(
// Product category filter
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category ) ),
'field' => 'slug',
),
// Product tag 1 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag1),
'field' => 'slug',
),
// Product tag 2 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag2),
'field' => 'slug',
),
// Product tag 3 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag3),
'field' => 'slug',
),
),
// Price filter
'meta_query' => array( array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
) ),
);测试和工作。
发布于 2019-02-21 10:46:22
我找到了更简单的方法来完成这一任务,因为当没有过滤器时,它将显示所有产品,查询也会更小,如下所示:
$category = 'category-slug-here'; //String, but can accept array?
$tags = 'comma,delimeted,tags,here'; //I build string with tags needed no array.
$clow = 0; //min price int
$chigh = 200; //max price int
$custom_query_args = array(
'posts_per_page' => '12',
'product_cat' => $category,
'post_type' => 'product',
'product_tag' => $tags,
// Price filter
'meta_query' => array( array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
) ),
);有什么不好的方面这样做,因为我已经测试了它使用不同的过滤器标签,它的工作方式,我计划呢?
https://stackoverflow.com/questions/54711351
复制相似问题