0

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?

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. 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. Commented Sep 25, 2017 at 14:25

2 Answers 2

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 );
1
  • the problem isn't with the number of posts being returned, it's with the taxonomy terms for each post. It's capped at 10 in the JSON result for each post item. If I tag something with 14 Skill taxonomy terms, it returns a max of 10 skill taxonomy terms. Commented Feb 22, 2019 at 22:07
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.