pagination in WP 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

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 but didn't get the idea. Can anyone explain to me with a simple code with pagination so that I can understand how it works? The following is the code that I have done till now:

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;
    }
} 

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 but didn't get the idea. Can anyone explain to me with a simple code with pagination so that I can understand how it works? The following is the code that I have done till now:

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;
    }
} 
Share Improve this question edited Sep 16, 2019 at 11:58 Matthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges asked Sep 16, 2019 at 5:52 Ajay PaudelAjay Paudel 1211 silver badge5 bronze badges 5
  • 1 You don't need a whole new endpoint just to get posts in descending order... – Jacob Peattie Commented Sep 16, 2019 at 6:03
  • no no i made it custom bcause i want to make with its own key i mean value with my own key so cn you explain how can i add pagination – Ajay Paudel Commented Sep 16, 2019 at 6:15
  • Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. – Matthew Brown aka Lord Matt Commented Sep 16, 2019 at 10:08
  • You could look at WP_REST_Posts_Controller::get_items() which does pagination for a posts query. It looks like it passes the page number from the query string as 'paged' in the options array. It also follows the usual WordPress REST API convention of adding X-WP-Total and X-WP-TotalPages headers with the number of posts and pages respectively. – Rup Commented Sep 16, 2019 at 23:15
  • 1 Thanks @MatthewBrownakaLordMatt and yes we have got the correct answer – Ajay Paudel Commented Sep 17, 2019 at 9:23
Add a comment  | 

1 Answer 1

Reset to default 1
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. This is the correct answer that we have got and has worked for me

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745152396a4613926.html

相关推荐

  • pagination in WP 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

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信