为什么 WordPress 显示来自空 post__in 数组的查询结果? [英] Why is WordPress showing query results from an empty post__in array?
问题描述
我有以下 WP_Query
参数:
$posts = new WP_Query(array(
'post__in' => $postids,
'meta_key' =>'ratings_average',
'orderby'=>'meta_value_num',
'order' =>'DESC',
));
$postids 是从另一个 WP_Query
检索的 id 数组.我的问题是,即使 $postids 为空,Wordpress 循环也会显示帖子.如果 $postids 为空,我该如何管理它不应该显示任何帖子.
$postids is an array of ids which is retrieved from another WP_Query
. My problem here is that even if $postids is empty, Wordpress loop shows posts. How can I manage this that it shouldn't show any post if $postids is empty.
推荐答案
这并不能直接解决 post__in
的问题,但我不明白为什么这不起作用..>
This isn't directly fixing the issue with post__in
but I don't see why this wouldn't work..
if(!empty($postids)){
$posts = new WP_Query(array(
'post__in' => $postids,
'meta_key' =>'ratings_average',
'orderby'=>'meta_value_num',
'order' =>'DESC',
));
} else {
//Do something else or nothing at all..
}
如您所见,WP_Query
调用只会在 $postids
中包含 value/s 时发生.如果没有,则不会调用 WP_Query
并且循环将永远不会发生,就像您的查询返回 0 个帖子一样.
as you can see the WP_Query
call will only happen if $postids
has value/s in it. if it doesn't, then no call is made to WP_Query
and the loop will just never happen, same as if your query returned 0 posts.
这篇关于为什么 WordPress 显示来自空 post__in 数组的查询结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!