WordPress REST API - more than 10 posts

kurtg picture kurtg · May 23, 2018 · Viewed 8.3k times · Source

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!

Answer

André Kelling picture André Kelling · Oct 11, 2018

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