WP_Query 不返回任何结果 [英] WP_Query returns no results

查看:56
本文介绍了WP_Query 不返回任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WP_Query 和一些参数在 wordpress 中检索一些页面:

I am trying to retrieve some pages in wordpress using WP_Query and some arguments:

$args = array(
   'post_type' => 'posttype',
   'posts_per_page' => 24,
   'post__in'      => $store_ids,
   'paged' => $paged,
   'post_status' => 'publish',
);

$the_query = new WP_Query( $args );

我在此处尝试检索的页面应与我提供的 ID 数组中的 ID 相匹配.数组和其他参数看起来不错,因为当我使用 get_posts 而不是 WP_Query 时确实得到了我的结果.这里出了什么问题?

The pages I'm trying to retrieve here should match an ID in an array of ID's I have given it. The array and other arguments seem fine since I do get my results when I use get_posts instead of WP_Query. What is going wrong here?

推荐答案

我的猜测是,您的主题中某​​处编写的过滤器写得不好,它作用于 WP_Query,而且很可能是操作 pre_get_posts.

My educated guess is that you have a poorly written filter somewhere in your theme that is acting on WP_Query, and it is most probably the action pre_get_posts.

get_posts 使用 WP_Query.唯一的区别是 get_posts 默认将以下两个参数传递给 WP_Query:

get_posts makes use of WP_Query. The only difference is that get_posts passes the following two arguments to WP_Query by default:

  • 'no_found_rows' =>true 分页失败",这就是为什么你不能分页 get_posts

  • 'no_found_rows' => true which "fails" pagination, that is why you can't paginate get_posts

'suppress_filters' =>true 这是很重要的一个,它的作用是阻止过滤器改变查询.所以pre_get_posts 和内置的posts_* 过滤器不能用于改变get_posts.这就是为什么在您的情况下您使用 get_posts 获得帖子而没有使用 WP_Query

'suppress_filters' =>true This is the important one, what this does is, it stops filters from altering the query. So pre_get_posts and the build in posts_* filters cannot be used to alter get_posts. This is why in your case you get posts using get_posts and none using WP_Query

这里的脏修复是添加 'suppress_filters' =>WP_Query 中的查询参数为 true.正确的解决方法是寻找改变查询的过滤器.正如我所说的,很可能 pre_get_posts 你没有使用 is_main_query() 检查来定位主查询

The dirty fix here is to add 'suppress_filters' => true to your query arguments in WP_Query. The correct fix will be is to look for the filter altering the query. As I said, most probably pre_get_posts where you did not use the is_main_query() check to just target the main query

这篇关于WP_Query 不返回任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆