user2568495
user2568495

Reputation:

WP_Query returns no results

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?

Upvotes: 4

Views: 9434

Answers (1)

user4717687
user4717687

Reputation:

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

Upvotes: 17

Related Questions