Search code examples
wordpresswordpress-themingwordpress-rest-api

WP pagination in rest api


we have made a custom api to get all the post in descending order and we want to add pagination in that api, I have read other questions and answers also but didnt get any idea so can some one explain me with a simple code with pagination so that I can understand how it works.

This is my code so how can I add pagination can anyone explain to me because I searched other questaion also but I didnt get any idea.

define('API_ENDPOINT_VERSION',1);

//flush the rewrite rules on plugin activation

function apiendpoint_activate()
{
    flush_rewrite_rules();
}
register_activation_hook(__FILE__,'apiendpoint_activate');

function apiendpoint_register_endpoints(){
    register_rest_route(
        'api/v1',
        '/post',
        [
            'methods' => 'GET',
            'callback' =>'api_get_post',
        ]
    );

}
add_action('rest_api_init','apiendpoint_register_endpoints');

function api_get_post($request){
    $ar = array( 'post_type'=>'post',
         'posts_per_page'=>15,
         'orderby' => 'date',
         'order' => 'DESC',
       );
    $posts = get_posts($ar);
    //var_dump($posts);
    //exit;
    $a = array();

    if($posts){
        foreach ($posts as $post) {


        $a[]= array(   
        'title'=>$post->post_title,
         'link'=>get_the_permalink($post->ID),
         'category'=>get_the_category($post->ID),
         'published_date'=>get_the_date('l, F j, Y',$post->ID),
          'guid'=>$post->guid,
          'image'=>get_the_post_thumbnail_url($post->ID,'large'),
           'description'=>$post->post_excerpt,
           'source'=>"Nepaljapan"
        //'img'=>$img
        );
        }
        return $a;
    }
}

Solution

  • Try the below code:

    define('API_ENDPOINT_VERSION', 1);
    
    //flush the rewrite rules on plugin activation
    
    function apiendpoint_activate() {
        flush_rewrite_rules(); } register_activation_hook(__FILE__, 'apiendpoint_activate');
    
    function apiendpoint_register_endpoints() {
        register_rest_route(
            'api/v1',
            '/post',
            [
                'methods' => 'GET',
                'callback' => 'api_get_post',
            ]
        );
    
    } add_action('rest_api_init', 'apiendpoint_register_endpoints');
    
    function api_get_post($request) {
        $ar = array('post_type' => 'posts',
            'posts_per_page' => 15,
            'orderby' => 'date',
            'order' => 'DESC',
            'paged' => ($_REQUEST['paged'] ? $_REQUEST['paged'] : 1) 
    ); 
    
    $posts = get_posts($ar); //var_dump($posts); //exit; $a = array();
    
    if ($posts) {
        foreach($posts as $post) {
    
    
            $a[] = array(
                'title' => $post -> post_title,
                'link' => get_the_permalink($post -> ID),
                'category' => get_the_category($post -> ID),
                'published_date' => get_the_date('l, F j, Y', $post -> ID),
                'guid' => $post -> guid,
                'image' => get_the_post_thumbnail_url($post -> ID, 'large'),
                'description' => $post -> post_excerpt,
                'source' => "Nepaljapan"
                //'img'=>$img
            );
        }
        return $a; } 
    }
    

    Call API call as below:

    /wp-json/api/v1/post?paged=1

    Increase the paged value by 1 to get next paging posts.

    Hope this helps!