这可能不是最好的答案,但就目前而言,我已经能够使用前面提到的WP\\U查询的结果,通过WP\\U REST\\U请求再次查询,获得我想要的结果。所以完整的解决方案如下所示。
function get_blog_posts() {
$posts = new WP_Query( array(
\'post_type\' => \'post\',
\'posts_per_page\' => 4,
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_format\',
\'operator\' => \'NOT EXISTS\',
),
),
));
$ids = [];
foreach($posts->posts as $post){
$ids[] = $post->ID;
}
$include = implode(",", $ids);
$request = new WP_REST_Request( \'GET\', \'/wp/v2/posts\' );
$request->set_query_params( [ \'include\' => $include ] ); // because WP_REST_Request doesn\'t allow you to have query parameters inline.
$response = rest_do_request( $request );
$response = $response->get_data(); // You may handle errors too
if (empty($response)) {
return null;
}
return $response;
}
add_action( \'rest_api_init\', function () {
register_rest_route( \'unicorn/v1\', \'/blog_posts\', array(
\'methods\' => \'GET\',
\'callback\' => \'get_blog_posts\',
));
});