Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active March 24, 2017 17:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/5a0e8606c9fa1aa2b709eaef90bdb33b to your computer and use it in GitHub Desktop.
Save westonruter/5a0e8606c9fa1aa2b709eaef90bdb33b to your computer and use it in GitHub Desktop.
<?php
// Make internal REST API requests for all resources the client JS app will need.
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_query_params( array(
'per_page' => 5,
'orderby' => 'title',
'order' => 'desc',
) );
$response = rest_get_server()->dispatch( $request );
$response = apply_filters( 'rest_post_dispatch', $response, rest_get_server(), $request );
$posts = rest_get_server()->response_to_data( $response, true /* _embed */ );
?>
<script>
wp.api.init().done( function() {
var posts = new wp.api.collections.Posts();
posts.reset( <?php echo wp_json_encode( $posts ) ?> );
// App is now ready to render immediately without having to wait for fetching.
} );
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment