在深入研究源代码之后,最终得到了这个解决方案。对我有用,也可能对其他人有用。
function get_rest_items_query($post_type, $posts_per_page, $orderby = \'date\', $order = \'desc\') {
$result = array();
$args = array( \'post_type\' => $post_type, \'posts_per_page\' => $posts_per_page, \'orderby\' => $orderby, \'order\' => $order );
$posts = get_posts($args);
$ids = implode(\',\', wp_list_pluck($posts, \'ID\'));
// request
$restRequest = new WP_REST_Request(\'GET\', \'/wp/v2/\' . $post_type );
$restRequest->set_param(\'include\', $ids);
// response
$response = rest_do_request( $restRequest );
// _embed
$rest = rest_get_server()->response_to_data( $response, true );
$result[\'posts\'] = $rest;
return $result;
}