我这样做了,而且很有效,但我不确定这是否是最好的方式:
function get_override_query() {
$posts = get_posts_from_some_custom_method();
$query = new WP_Query();
$query->current_post = -1;
$query->post_count = count( $posts );
$query->posts = $posts;
return $query;
}
function override_query() {
global $wp_query;
$query = get_override_query();
$wp_query = $query;
}
然后,我还可以在不更改默认查询的情况下遍历帖子,如:
$query = get_override_query();
while ( $query->has_posts() ) {
$query->the_post();
...
}