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?
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 actionpre_get_posts
.get_posts
makes use ofWP_Query
. The only difference is thatget_posts
passes the following two arguments toWP_Query
by default:'no_found_rows' => true
which “fails” pagination, that is why you can’t paginateget_posts
'suppress_filters' =>true
This is the important one, what this does is, it stops filters from altering the query. Sopre_get_posts
and the build inposts_*
filters cannot be used to alterget_posts
. This is why in your case you get posts usingget_posts
and none usingWP_Query
The dirty fix here is to add
'suppress_filters' => true
to your query arguments inWP_Query
. The correct fix will be is to look for the filter altering the query. As I said, most probablypre_get_posts
where you did not use theis_main_query()
check to just target the main query