DEV Community

Discussion on: How to get all WordPress posts from the WP API with JavaScript

Collapse
 
joshuaburleson profile image
Joshua Burleson • Edited

Nice, I've taken a similar approach on posts from multiple sources before. I would just like to point out that this use case is one of the ones where the backend, assuming you control it [ even though it's PHP (gross) ] is probably the better way to go ( primarily because the response time will be much faster than doing multiple queries ). You can use a filter on the rest_[your-post-type]_collections_params like so

function cpt_custom_max_per_page( $params ) {
    if ( isset( $params['per_page'] ) ) {
        $params['per_page']['maximum'] = *value of your choice*;
    }
    return $params;
}

$cpts = ['dancers', 'team_members', 'crew_members', 'locations', 'cats', 'pages'];

foreach( $cpts as $cpt ){
    $post_params_hook = 'rest_' . $cpt . '_collection_params';
    add_filter( $post_params_hook, 'cpt_custom_max_per_page', 10, 1 );
}
Enter fullscreen mode Exit fullscreen mode