问题
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' => truewhich "fails" pagination, that is why you can't paginateget_posts'suppress_filters' =>trueThis is the important one, what this does is, it stops filters from altering the query. Sopre_get_postsand the build inposts_*filters cannot be used to alterget_posts. This is why in your case you get posts usingget_postsand none usingWP_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