6

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 ??
4
  • you can retrieve a list of posts and its properties with this function codex.wordpress.org/Function_Reference/get_posts
    – mmm
    Jan 17 '18 at 11:23
  • get_posts() is the same as WP_Query()->posts. What I'm looking for is Rest API json result without using curl or file_get_content() as it makes extra request to server.
    – Janiis
    Jan 17 '18 at 11:49
  • WP_Query (and so get_posts()) don't use a HTTP request, they search directly in the database then this code doesn't make an extra request.
    – mmm
    Jan 17 '18 at 12:04
  • True. But get_posts() dataset is different from wp-json/wp/v2/posts/?_embed results.
    – Janiis
    Jan 17 '18 at 12:11
4

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().

3
  • Also check out the User Contributed Notes in there by Ryan McCue, e.g. the error checks, like $response->is_error(), before using the data.
    – birgire
    Jan 17 '18 at 15:49
  • In case, if you need to add params to WP_REST_Request, you can use $request->set_param('per_page', 10);
    – Janiis
    Jan 18 '18 at 7:20
  • 1
    yes and for multiple query arguments one can use $request->set_query_params( [ 'per_page' => 10, ... ] ); but _embed can't be activated that way.
    – birgire
    Jan 18 '18 at 8:13
0

After digging into source code ended up with this solution. Works for me, might be useful to others as well.

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;
}
1
  • 1
    Note that this is not efficient. By doing get_posts() you make a query to get all posts and by doing REST requests you make another query per post.
    – swissspidy
    Jan 24 '18 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.