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