Our plugin support pagination by default. However, since there are various types of requests and responses for different API types, we have implemented filters to assist in identifying the exact parameters required for pagination to function correctly. You can add the necessary code snippets to your theme’s functions.php file, or utilize the Code Snippets plugin.
wpgetapi_pre_api_to_posts_pagination_next_value
This filter is used to find the exact pagination parameter from the API response and return the next value according to that.
function wpgetapi_pagination_next_value( $next, $data, $api_id, $endpoint_id ) {
//Here we return next page number or URL for pagination
return $next;
}
add_filter( 'wpgetapi_pre_api_to_posts_pagination_next_value', 'wpgetapi_pagination_next_value', 10, 4 );
Alternatively,
wpgetapi_api_to_posts_pagination_next_value
function wpgetapi_pagination_next_value( $next, $data, $api_id, $endpoint_id ) {
//Here we return next page number or URL for pagination
return $next;
}
add_filter( 'wpgetapi_api_to_posts_pagination_next_value', 'wpgetapi_pagination_next_value', 10, 4 );
wpgetapi_pre_api_to_posts_pagination_type
API pagination methods vary; sometimes the API provides a URL for the next page, while other times it offers the next page number directly. Additionally, the variable for the next page number may differ between APIs. To address these variations, a filter is utilized to define the type of pagination for the next page and specify what is passed to the query variable.
function wpgetapi_pagination_type( $type, $api_id, $endpoint_id ) {
//Here we return $type based on previous code
//If it is a page number then we return page number variable which we use in url
//If it is a URL then we just return "url" string.
return $type;
}
add_filter( 'wpgetapi_pre_api_to_posts_pagination_type', 'wpgetapi_pagination_type', 10, 3 );
Alternatively,
wpgetapi_pre_api_to_posts_pagination_type
function wpgetapi_pagination_type( $type, $api_id, $endpoint_id ) {
//Here we return $type based on previous code
//If it is a page number then we return page number variable which we use in url
//If it is a URL then we just return "url" string.
return $type;
}
add_filter( 'wpgetapi_api_to_posts_pagination_type', 'wpgetapi_pagination_type', 10, 3 );
API pagination methods vary; sometimes the API provides a URL for the next page, while other times it offers the next page number directly. Additionally, the variable for the next page number may differ between APIs. To address these variations, a filter is utilized to define the type of pagination for the next page and specify what is passed to the query variable.
- For instance, when the API endpoint follows a structure like this: https://api.quotable.io/quotes?page=2 and requires the query variable page for pagination, there’s no need to add the filter because our plugin’s default query variable for pagination is page.
- However, when dealing with an API structured like this: https://api.quotable.io/quotes?pageNumber=2 and needing to pass the query variable pageNumber (or other variables depending on the API) for pagination, it’s necessary to add the below filter code.
Example 1 – Response with page number
If we have data in API response like current page, next page, total pages, or total items and a pagination URL like this https://api.example.com/products?page_number=2
{
"products": [
{
"id": 1,
"name": "Product 1",
"price": 19.99,
"category": "Electronics"
},
{
"id": 2,
"name": "Product 2",
"price": 29.99,
"category": "Clothing"
}
],
"pagination": {
"current_page": 1,
"total_pages": 5,
}
}
The below code will be used when there is pagination data like current page number and total pages in response. Here we have to change variables according to API Response
function wpgetapi_pagination_next_value( $next, $data, $api_id, $endpoint_id ) {
//We have to pass all keys to reach page_number and total_pages like. $data['key1']['key2']
if ( ( isset( $data['pagination']['current_page'] ) && isset( $data['pagination']['total_pages'] ) ) && $data['pagination']['current_page'] < $data['pagination']['total_pages'] ) {
return $data['pagination']['current_page'] + 1;
}
return false;
}
add_filter( 'wpgetapi_pre_api_to_posts_pagination_next_value', 'wpgetapi_pagination_next_value', 10, 4 );
function wpgetapi_pagination_type( $wpgetapi, $type = 'page' ) {
//Here we return only string with variable we need to pass in url
//Default is 'page'
return 'page_number';
}
add_filter( 'wpgetapi_pre_api_to_posts_pagination_type', 'wpgetapi_pagination_type', 10, 3 );
Example 2 – Response with next page URL
If we have data in response with the next page URL like below example
{
"products": [
{
"id": 1,
"name": "Product 1",
"price": 19.99,
"category": "Electronics"
},
{
"id": 2,
"name": "Product 2",
"price": 29.99,
"category": "Clothing"
}
],
"pagination": {
"next_page_url": "https://api.example.com/products?page=2",
"prev_page_url": null
}
}
The code below will be used when pagination data is available with the next page URL. Here we have to change the variable according to API Response.
function wpgetapi_pagination_next_value( $next, $data, $api_id, $endpoint_id ) {
//We have to pass all keys to reach Next url
if ( isset( $data['pagination']['next_page_url'] ) && ! empty( $data['pagination']['next_page_url'] ) ) {
return $data['pagination']['next_page_url'];
}
return false;
}
add_filter( 'wpgetapi_pre_api_to_posts_pagination_next_value', 'wpgetapi_pagination_next_value', 10, 4 );
function wpgetapi_pagination_type( $type, $api_id, $endpoint_id ) {
//When we receive a Next page url in the response, it’s necessary to add this filter and return just the 'url'.
return 'url';
}
add_filter( 'wpgetapi_pre_api_to_posts_pagination_type', 'wpgetapi_pagination_type', 10, 3 );
Alternatively, can use the below filters
add_filter( 'wpgetapi_api_to_posts_pagination_next_value', 'wpgetapi_api_to_posts_pagination_next', 10, 4 );
add_filter( 'wpgetapi_api_to_posts_pagination_type', 'wpgetapi_api_to_posts_pagination_type', 10, 3 );