I have created a custom restful API endpoint in WordPress which returns the JSON with the only required fields.
So with this one when I go to the example.com/wp-json/wl/posts, it returns 5 posts as I have limited the number of the posts.
function wl_posts() { $args = [ 'numberposts' => 99999, 'post_type' => 'post' ]; $posts = get_posts($args); $data = []; $i = 0; foreach($posts as $post) { $data[$i]['id'] = $post->ID; $data[$i]['title'] = $post->post_title; $data[$i]['content'] = $post->post_content; $data[$i]['slug'] = $post->post_name; $data[$i]['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'thumbnail'); $i++; } return $data; } add_action('rest_api_init', function() { register_rest_route('wl/v1', 'posts', [ 'methods' => 'GET', 'callback' => 'wl_posts', ]); }); But I also want to add the pagination, so if I add ?page=2, it should return another 5 posts.
How can that be archived?