How to limit get_posts()?

You can use posts_per_page or numberposts. http://codex.wordpress.org/Template_Tags/get_posts#Parameters EDIT: Regarding your edit. You cannot have the posts_per_page different to the LIMIT in the SQL query. get_posts() creates an instance of WP_Query and returns it using the posts_per_page as the LIMIT.

data returned from get_post($postId) have different keys than wp-json/wp/v2/posts/{postId}

You can set your own keys in the JSON response array. It can be anything. Take a look at this simple example: add_action( ‘rest_api_init’, function () { register_rest_route( ‘shubham’, ‘/get_the_post/’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘get_posts_by_rest’ ) ); }); // Callback function function get_posts_by_rest( ){ // Get the post id from the URL and … Read more