我正试图根据产品属性(分类法)从WooCommerce获取特定产品。
$args = array(
\'post_type\' => array(\'product\', \'product_variation\'),
\'post_status\' => \'publish\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'pa_range-kwh\',
\'terms\' => \'k2\',
\'field\' => \'slug\',
\'operator\' => \'IN\',
\'include_children\' => true,
),
),
);
从SQL的角度来看,我可以看到:
SELECT * FROM `wp_terms` WHERE term_id = 891
退货
SELECT * FROM `wp_term_taxonomy` where term_id = 891
退货
SELECT * FROM `wp_term_relationships` where term_taxonomy_id = 891
退货
虽然至少有一个产品添加了分类法,但由于内部查询返回(0=1),我一直收到0个结果,这意味着它找不到任何具有此分类法属性的产品。
我能做些什么来克服这个问题?
P、 S.完整功能
$GLOBALS[\'SERVER_SIDE_EQUATIONS\'][\'get_products\'] = function(){
$products = \'\';
$argsAlt = array(
\'post_type\' => array(\'product\', \'product_variation\'),
\'post_status\' => \'publish\',
\'suppress_filters\' => true,
\'range-kwh\' => \'k2\',
\'pa_range-kwh\' => \'k2\',
\'lazy_load_term_meta\' => false,
\'tax_query\' => array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'pa_range-kwh\',
\'terms\' => array(\'k2\'),
\'field\' => \'slug\',
\'operator\' => \'IN\',
\'include_children\' => true,
),
),//end tax_array
);
$queryAlt = new WP_Query( $argsAlt );
if($queryAlt->have_posts()){
$products .=\'yes \';
$products .= $custom_posts->found_posts. "<br />";
$_pf = new WC_Product_Factory();
foreach($queryAlt->posts as $post){
$product_obj = $_pf->get_product($post->ID);
$products .= $product_obj->get_title();
} //end foreach
}else{
$products .= \'<br />There are no products that satisfy the search criteria.\';
}//end if
return $products;
};