Because large queries can hurt site performance, WordPress caps per_page REST parameter at 100 records. However, there are situations when you need to override this value.

I saw solutions similar to this one, but what I dislike is that they modify the SQL query to ignore the limit set by the client. Thus, even if the client asks for 10 records, such solutions will modify the query to return much more rows. This definitely hurts site performance 🙂

Luckily, the proper solution is easy. There is a hook to filter collection parameters for the posts controller.

$port_type = 'attachment';

add_filter("rest_{$post_type}_collection_params", function($params) {
    $params['per_page']['maximum'] = 500;
    return $params;
});

In the example above, the maximum value of per_page for media (/wp-json/wp/v2/media) is set to 500 records.

How to Increase per_page Limit in WordPress REST API
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *