This is simple posts result array.

$query = new WP_Query( array( 'post_type' => 'post' ) );
$posts = $query->posts; // returns simple array of data

Is there a way to get the results from wp-json/wp/v2/posts/?_embed without making extra request to server to pull json and then decode to php array?

Looking for something like that:

$posts = $query->rest_posts(); // for example ??

2 s
2

I think we can simplify it with:

$request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$response = rest_do_request( $request );
$data     = rest_get_server()->response_to_data( $response, true );

by using rest_do_request().

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *