我正在创建一个产品过滤器,我一直在努力找出如何使此查询正常工作。我需要能够向这个查询提供一个术语数组(woocommerce产品属性),并需要它返回all 提供的条款。
到目前为止,它似乎返回了任何条款的产品。如有任何建议,将不胜感激。
以下是我迄今为止的论点:
$selectedOptions = array(\'test-attribute\', \'test3\');
$args=array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'pa_filterable-attribute\',
\'terms\' => $selectedOptions,
\'field\' => \'slug\',
\'operator\' => \'IN\'
)
)
);
如果重要的话,这里是我的循环:
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
woocommerce_product_loop_start();
woocommerce_product_subcategories();
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php wc_get_template_part( \'content\', \'product\' ); ?>
<?php endwhile;
woocommerce_product_loop_end();
}
wp_reset_query();
最合适的回答,由SO网友:Fencer04 整理而成
我相信,将您的操作员更改为“和”,如下所示将有效:
$selectedOptions = array(\'test-attribute\', \'test3\');
$args=array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'pa_filterable-attribute\',
\'terms\' => $selectedOptions,
\'field\' => \'slug\',
\'operator\' => \'AND\'
)
)
);