#42671 closed enhancement (wontfix)
REST - getting all taxonomy terms with one GET request causes division by zero warning.
Reported by: | tmuikku | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | |
Focuses: | Cc: |
Description
Querying taxonomy terms from REST API one can affect the returned items number by using per_page REST argument, like so:
wp-json/wp/v2/customtaxonomy?per_page={number}
Minimum value for this argument is 1, which is annoying in the taxonomy terms context. One have to use a filter to change per_page minimum value to 0 to be able to get all terms at once.
The per_page argument for taxonomy terms route is set in WP_REST_Terms_Controller parent WP_REST_Controller, see get_collection_params() method.
Here is an filter example to allow per_page value 0:
<?php add_filter( "rest_customtaxonomy_collection_params", 'myplugin_rest_customtaxonomy_collection_params_filter'); function myplugin_rest_customtaxonomy_collection_params_filter($query_params) { $query_params['per_page']['minimum'] = 0; return $query_params; }
Now all terms are returned with GET request:
wp-json/wp/v2/customtaxonomy?per_page=0
But now a PHP warning "Division by zero" emerges in
wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php:271
Please, fix the division by zero warning.
Preferably, also set the per_page minimum value to 0 in WP_REST_Terms_Controller::get_collection_params()
Thanks!
Hi @tmuikku,
Apologies for the delay in providing a response.
The REST API does not support unbounded pagination. Instead, it is recommended to query through subsequent pages or increasing the maximum to a value that works for your website.
This was discussed at length during the Gutenberg project. I think this PR is a good jumping off place to read more about the discussion: https://github.com/WordPress/gutenberg/pull/10762