Increasing wordpress per_page maximum (Fixes Invalid parameter(s): per_page)
Have you run into the issue where WordPress won’t return the number of results you’re seeking through the API? The api accepts a per_page parameter, however WordPress has this value capped at 100. That’s a pretty low maximum and unfortunately they’ve made it difficult to change it. I found various suggestions to use this filter, or that filter, but none worked because they are applied after the per_page maximum check runs. I had to dig deep to find a filter which runs at the right time.
This code increases the per_page maximum for the endpoint /wc/v3/products to 512, but it should be easy to modify for other endpoints and values.
Hopefully I can save you the effort, here’s the code:
add_filter( 'rest_endpoints', 'increase_rest_endpoint_per_page_maximum', 10, 1 ); function increase_rest_endpoint_per_page_maximum( $endpoints ) { foreach($endpoints as $endpoint => $methods){ if($endpoint === '/wc/v3/products') { foreach ($methods as $method => $options){ $hasArgs = array_key_exists('args', $options); $hasPerPage = $hasArgs && array_key_exists('per_page', $options['args']); if($hasArgs && $hasPerPage){ $endpoints[$endpoint][$method]['args']['per_page']['maximum'] = 512; } } } } return $endpoints; }
Recent Comments