I'm trying to use the REST API v2 to populate a modal window when clicking on a project custom post type item.
Projects have a custom taxonomy, Skills. I'm using ?_embed on the JSON URL and it is returning the custom taxonomy items, but it's being limited to 10 terms for each returned Project item. I can't seem to do anything to get ALL of the tagged Skills to return for a given Project item.
I updated the general pagination settings in the WordPress settings to be 20, thinking that would adjust the number of associated taxonomy terms returned: no change.
I added the following method to make the taxonomy accessible via REST:
add_action( 'init', 'my_custom_taxonomy_rest_support', 25 );
function my_custom_taxonomy_rest_support() {
global $wp_taxonomies;
$taxonomy_name = 'skill';
if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
$wp_taxonomies[ $taxonomy_name ]->posts_per_page = -1;
}
}
And I've tried all variations of per_page, posts_per_page and using -1, 0, 99 to no avail. I can't seem to find anything about this being an issue for anyone else so I'm at a bit of a loss of what to do here. If a project is tagged with 14 skills, the most I can ever get back from the REST API is 10 Skill taxonomy terms for each Project.
Anyone know how to change the limit to be unlimited?
I'm trying to use the REST API v2 to populate a modal window when clicking on a project custom post type item.
Projects have a custom taxonomy, Skills. I'm using ?_embed on the JSON URL and it is returning the custom taxonomy items, but it's being limited to 10 terms for each returned Project item. I can't seem to do anything to get ALL of the tagged Skills to return for a given Project item.
I updated the general pagination settings in the WordPress settings to be 20, thinking that would adjust the number of associated taxonomy terms returned: no change.
I added the following method to make the taxonomy accessible via REST:
add_action( 'init', 'my_custom_taxonomy_rest_support', 25 );
function my_custom_taxonomy_rest_support() {
global $wp_taxonomies;
$taxonomy_name = 'skill';
if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
$wp_taxonomies[ $taxonomy_name ]->posts_per_page = -1;
}
}
And I've tried all variations of per_page, posts_per_page and using -1, 0, 99 to no avail. I can't seem to find anything about this being an issue for anyone else so I'm at a bit of a loss of what to do here. If a project is tagged with 14 skills, the most I can ever get back from the REST API is 10 Skill taxonomy terms for each Project.
Anyone know how to change the limit to be unlimited?
You can try this
/*
* Increase the limit of posts per page in json
*/
add_filter( 'rest_your_collection_params', function ( $params, WP_Post_Type $post_type ) {
if ( 'your_cpt' === $post_type->name && isset( $params['per_page'] ) ) {
$params['per_page']['maximum'] = 99;
}
return $params;
}, 10, 2 );
Referreing to this: "When embedding a collection response (...) the embedded collection will have the default pagination limits applied."
If you did not set those parameters for the taxonomy rest route than this probably defaults to 10.
Besides that, you could filter the per_page parameter for all embeds:
/** change default limit of embeds within rest requests
* @link https://github.com/WordPress/WordPress/blob/5.6-branch/wp-includes/rest-api/class-wp-rest-server.php#L1123
* @link https://developer.wordpress.org/reference/classes/wp_rest_request/get_param/
* @link https://developer.wordpress.org/reference/classes/wp_rest_request/set_param/
*/
function na_rest_embeds_set_per_page_param($response, $handler, $request) {
if($request->get_param('context') === 'embed') {
$request->set_param('per_page',100);
}
}
add_filter( 'rest_request_before_callbacks', 'na_rest_embeds_set_per_page_param', 10, 3 );
Hook in here, test if context is embed and then set the per_page parameter to e.g. 100.
"nopaging" => 1
in the request – mmm Commented Sep 25, 2017 at 8:37hide_empty
argument wich can filter results : developer.wordpress.org/reference/classes/wp_term_query/… – mmm Commented Sep 25, 2017 at 14:01