您好我正在尝试执行此查询,提取具有3个属性的物联网商品(Collezione,Finitura,Pietre)。但我们可以设置无,1,2或3个过滤器,所以我想问一下,如果没有设置过滤器,我必须设置哪个值。我认为-1但这不适用于此。
下面你可以找到我的代码。提前谢谢。
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cat_id,
'operator' => 'IN'
),
array(
'taxonomy' => 'pa_collezione',
'field' => 'term_taxonomy_id',
'terms' => $pa_collezione,
'operator' => 'IN'
),
array(
'taxonomy' => 'pa_finitura',
'field' => 'term_taxonomy_id',
'terms' => $pa_finitura,
'operator' => 'IN'
),
array(
'taxonomy' => 'pa_pietre',
'field' => 'term_taxonomy_id',
'terms' => $pa_pietre,
'operator' => 'IN'
)
)
);
$products = get_posts( $args );
答案 0 :(得分:0)
我认为您的条款类似于' pa_collezione'是您的 product_cat 分类标准。
您的tax_query应该只有一个术语字段的术语数组。
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $array_of_term_ids,
),
)
您在条款数组中执行过滤,而不是添加额外的关系。
答案 1 :(得分:0)
如果未设置参数,我发现解决方案设置为'NOT IN'而不是'IN'。 以下是解决方案。
$cat_id = $_POST['category'];
$pa_finitura= $_POST['finitura'];
$pa_collezione= $_POST['collection'];
$pa_pietre= $_POST['pietre'];
if($pa_collezione=="-1"){$cond_collezione='NOT IN';}else{$cond_collezione = 'IN';}
if($pa_finitura=="-1"){$cond_finitura='NOT IN';}else{$cond_finitura = 'IN';}
if($pa_pietre=="-1"){$cond_pietre='NOT IN';}else{$cond_pietre = 'IN';}
define('WP_USE_THEMES', false);
require_once('../../../../wp-load.php');
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $cat_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'pa_collezione',
'field' => 'term_taxonomy_id', //This is optional, as it defaults to 'term_id'
'terms' => $pa_collezione,
'operator' => $cond_collezione // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'pa_finitura',
'field' => 'term_taxonomy_id', //This is optional, as it defaults to 'term_id'
'terms' => $pa_finitura,
'operator' => $cond_finitura // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'pa_pietre',
'field' => 'term_taxonomy_id', //This is optional, as it defaults to 'term_id'
'terms' => $pa_pietre,
'operator' => $cond_pietre // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = get_posts( $args );