我有大多数观看过的帖子(在客户类别中),在主页中我添加了这个小部件并且它工作得很完美,详细页面也很完美但在类别页面中相同的小部件表现得很奇怪
$r = new WP_Query( array( 'tax_query' =>
array(
'relation' => 'OR',
array(
'taxonomy' => 'custcategory',
'field' => 'term_id',
'terms' => array(10)),
),
'category__in'=>array(10),
'post_type'=> $post_type ,
'posts_per_page' => $number,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC' ) );
if ($r->have_posts()) :
// Enters this block in home page and detail page
while ( $r->have_posts() ) : $r->the_post();
else:
// Enters this block in category page
任何人都知道为什么会出现这种奇怪的行为?
答案 0 :(得分:0)
也许您有一些影响类别页面的过滤器,将其添加到您的查询参数:
'suppress_filters' => true
您也可以尝试在主查询后添加wp_reset_query(),但这不是必需的,因为您声明了一个新的WP_Query。
答案 1 :(得分:0)
我只是意识到你的查询args是错误的。您已设置关系,但您只有一个分类数组。此外,您不需要'category__in'=>array(10)
参数,它仅适用于默认分类法“类别”,并且您只需要来自自定义分类“custcategory”的帖子,不是吗?
您的查询应该是:
$r = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'custcategory',
'field' => 'term_id',
'terms' => array(10)
),
),
'post_type'=> $post_type ,
'posts_per_page' => $number,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));