admin管理员组文章数量:1322872
I am using this code to get all posts under the category (name: 'Quizes', slug:'quizes'), which is a customized taxonomy (the slug is st_ai_cat).
// the query
$term_name = get_query_var('taxonomy'); // current taxonomy
$cat_name = get_query_var( 'term' ); // curent cat name
echo $term_name;
echo $cat_name;
$wp_query = new WP_Query(array('post_type'=>'st_ai',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $term_name,
'field' => 'slug',
'term' => $cat_name,
)
)
));
if ( $wp_query->have_posts() ) {
echo "good";
}
?>
But it always shows nothing. If I remove tax_query, then it can show all posts under the customized taxonomy. But i want to only get posts under the category of Quizes. Why this happens? I checked its usage again.
Thank you.
I am using this code to get all posts under the category (name: 'Quizes', slug:'quizes'), which is a customized taxonomy (the slug is st_ai_cat).
// the query
$term_name = get_query_var('taxonomy'); // current taxonomy
$cat_name = get_query_var( 'term' ); // curent cat name
echo $term_name;
echo $cat_name;
$wp_query = new WP_Query(array('post_type'=>'st_ai',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $term_name,
'field' => 'slug',
'term' => $cat_name,
)
)
));
if ( $wp_query->have_posts() ) {
echo "good";
}
?>
But it always shows nothing. If I remove tax_query, then it can show all posts under the customized taxonomy. But i want to only get posts under the category of Quizes. Why this happens? I checked its usage again.
Thank you.
Share Improve this question asked Sep 20, 2020 at 16:15 Shark DengShark Deng 1055 bronze badges1 Answer
Reset to default 1Try to change $cat_name by the category ID.
$term_name = get_query_var('taxonomy'); // current taxonomy
$cate = get_queried_object();
$cat_id = $cate->term_id; // current category ID
echo $term_name;
echo $cat_id;
$wp_query = new WP_Query(array('post_type'=>'st_ai',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $term_name,
'field' => 'slug',
'term' => $cat_id,
)
)
));
if ( $wp_query->have_posts() ) {
echo "good";
}
?>
本文标签: wp queryWhy taxquery in WPQuery not working
版权声明:本文标题:wp query - Why tax_query in WP_Query not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742108288a2421125.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论