WP_Query returns no results

独自空忆成欢 提交于 2019-12-24 03:34:27

问题


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 );

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?


回答1:


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 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 which "fails" pagination, that is why you can't paginate get_posts

  • '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

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



来源:https://stackoverflow.com/questions/30213495/wp-query-returns-no-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!