我正在尝试为wooccommerce制作按属性的ajax过滤器。当我添加tax_query
时,问题是查询给出了空结果。此代码位于functions.php 中
$newquery = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => '22',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'pa_chrisi', // Product attribute taxonomy: always start with 'pa_'
'field' => 'term_id', // Can be 'term_id', 'slug' or 'name'
'terms' => 170,
), ),
) );
我已经检查了数据库中的pa_chrisi分类法。我试过:
- 在我的代码之前初始化wooccommerce(不确定为什么(
- 添加
'suppress_filters' => true,
和'include_children' => false,
echo $newquery->request;
给我
SELECT SQL_CALC_FOUND_ROWS dmg0j_posts.ID FROM dmg0j_posts WHERE 1=1 AND ( 0 = 1 ) AND dmg0j_posts.post_type = 'product' AND ((dmg0j_posts.post_status = 'publish')) GROUP BY dmg0j_posts.ID ORDER BY dmg0j_posts.post_date DESC LIMIT 0, 22
如果我删除tax_query
部分,它工作得很好。我得到了所有的产品。
请帮帮我!
解决方案:如果您在函数中使用WP_Query和tax_Query。php将代码放入低优先级的操作中:
add_action( 'init', 'maratgenius', 999 );
function maratgenius()
{
$newquery = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => '-1',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'pa_chrisi', // Product attribute taxonomy: always start with 'pa_'
'field' => 'term_id', // Can be 'term_id', 'slug' or 'name'
'terms' => $_POST['get_what'],
), ),
) );
//other code
}
作为替代方案,您可以在查询前通过以下方式注册WC分类:
WC_Post_Types::register_taxonomies();