kurtg
kurtg

Reputation: 83

WordPress REST API - more than 10 posts

I searched this issue and tried several solution with no luck.

My main route is here: https://cnperformance.wpengine.com/wp-json/wp/v2/products?_embed

I installed the 'WP REST API filter parameter' plugin to restore filter removed when REST API moved to WordPress core.

I've tried: https://cnperformance.wpengine.com/wp-json/wp/v2/products?_embed&?filter[per_page]=-1

and

https://cnperformance.wpengine.com/wp-json/wp/v2/products?_embed&?filter[posts_per_page]=-1

I've also tried this in functions.php

add_filter( 'rest_endpoints', function( $endpoints ){
    if ( ! isset( $endpoints['/wp/v2/products'] ) ) {
        return $endpoints;
    }
    unset( $endpoints['/wp/v2/products'][0]['args']['per_page']['maximum'] );
    return $endpoints;
});

reference here: https://github.com/WP-API/WP-API/issues/2316

I've set the value of posts_per_page to 100, -1, didn't make a difference. I also tried just adding the parameters '&posts_per_page=-1 without the filter query and that didn't work either. Any help or insights greatly appreciated!

Upvotes: 8

Views: 19467

Answers (4)

Marco
Marco

Reputation: 21

In my experience (... very long with WP :-)) ) and in all the thousands of attempts I have made (banging my head against the wall), it is not possible to bypass the 10 post limit with the WP rest API. The solution that really works is to create a custom endpoint using WP_Query in the WP server that provides the rest API.

  1. create the appropriate function:
function get_all_custom_post($data)
{
    $per_page = $data['per_page'] ? (int) $data['per_page'] : 100; // choose your limit

    // Run a custom query without the default limit
    $query = new WP_Query([
        'post_type'      => 'YOURCUSTOMPOST', // name of your custom post type here
        'posts_per_page' => $per_page,
    ]);

    // Retrieve results
    $all_custom_post = [];
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();

            // Get the metadata and thumbnail ID
            $post_id       = get_the_ID();
            $thumbnail_id  = get_post_thumbnail_id($post_id);
            $thumbnail_url = '';

            // Get the URL of the featured image if present
            if ($thumbnail_id) {
                $thumbnail     = wp_get_attachment_image_src($thumbnail_id, 'medium');
                $thumbnail_url = $thumbnail ? esc_url($thumbnail[0]) : '';
            }

            // Add custom post data to array with metadata and featured image
            $all_custom_post[] = [
                'id'             => $post_id,
                'title'          => get_the_title(),
                'content'        => get_the_content(),
                'meta'           => get_post_meta($post_id),
                'featured_image' => $thumbnail_url,
            ];
        }
    }
    wp_reset_postdata();

    return new WP_REST_Response($all_custom_post, 200);
}
  1. Register custom API REST route
function register_custom_post_endpoint()
{
    register_rest_route('custom-api/v1', '/myendpoint', [
        'methods'  => 'GET',
        'callback' => 'get_all_custom_post',
        'args'     => [
            'per_page' => [
                'default'           => 50,
                'validate_callback' => function ($param) {
                    return is_numeric($param);
                },
            ],
        ],
    ]);
}
add_action('rest_api_init', 'register_custom_post_endpoint');

This solution allows you to customize the results and above all.... it works!

Upvotes: 0

realmag777
realmag777

Reputation: 2088

Addition to this article if you need to get more tham 100 articles at once, into file functions.php of the current wordpress theme add next code:

   add_filter('rest_post_collection_params', function ($params, $post_type) {
        if (isset($params['per_page'])) {
            $params['per_page']['maximum'] = 999;//edit it as you want
        }
    
        return $params;
    }, 10, 2);

Upvotes: 0

zawhtut
zawhtut

Reputation: 8561

If per_page is not working, use filter[limit] for example

https://cnperformance.wpengine.com/wp-json/wp/v2/products/?filter[limit]=100

Upvotes: 2

André Kelling
André Kelling

Reputation: 1746

yes, you can fetch more then the default 10 posts at once.

just add the per_page parameter to your request.

example: https://cnperformance.wpengine.com/wp-json/wp/v2/products/?per_page=100

while 100 is the current max limit!

more info: https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/


example how load more then 100 items at once

with a for loop and information of the total pages amount after your first request:

https://github.com/AndreKelling/mapple/blob/master/public/js/mapple-public.js#L46

Upvotes: 19

Related Questions