Return all custom taxonomy terms for post in REST API v2? Currently limited to 10 terms

admin2025-01-07  5

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?

Share Improve this question edited Feb 22, 2019 at 22:09 butterednapkins asked Sep 24, 2017 at 15:03 butterednapkinsbutterednapkins 161 silver badge5 bronze badges 4
  • try to add "nopaging" => 1 in the request – mmm Commented Sep 25, 2017 at 8:37
  • @mmm no change. Only the first 10 are returned with the custom post type entry. – butterednapkins Commented Sep 25, 2017 at 11:50
  • sorry I was confusing with post request. for term request, there is the hide_empty argument wich can filter results : developer.wordpress.org/reference/classes/wp_term_query/… – mmm Commented Sep 25, 2017 at 14:01
  • That doesn't have anything to do with this either. The terms aren't empty, they're assigned to the current custom post type item. But the REST API will not return more than the first 10 custom taxonomy terms that a custom post type item has in the JSON for the custom post type item. the _embedded:['wp:term'][0] array is apparently limited to 10 terms and that can't be changed. – butterednapkins Commented Sep 25, 2017 at 14:25
Add a comment  | 

2 Answers 2

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265364a1040.html

最新回复(0)