Skip to content

Commit 1aa41de

Browse files
committed
REST API: Terms: Respect taxonomy's default query args.
It is possible to supply a set of default query `args` to `register_taxonomy()` which will be used when querying a list of terms -- for example, `orderby` in order to specify how the resulting list of terms should be sorted. The Terms REST API controller previously respected these default query args only if the request included a post ID. This changeset makes it so that the default args will also be respected if no post ID is provided. Props bernhard-reiter, jsnajdr. Fixes #62500. git-svn-id: https://develop.svn.wordpress.org/trunk@59458 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 27c566f commit 1aa41de

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public function get_items_permissions_check( $request ) {
227227
* Retrieves terms associated with a taxonomy.
228228
*
229229
* @since 4.7.0
230+
* @since 6.8.0 Respect default query arguments set for the taxonomy upon registration.
230231
*
231232
* @param WP_REST_Request $request Full details about the request.
232233
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
@@ -295,6 +296,22 @@ public function get_items( $request ) {
295296
}
296297
}
297298

299+
/*
300+
* When a taxonomy is registered with an 'args' array,
301+
* those params override the `$args` passed to this function.
302+
*
303+
* We only need to do this if no `post` argument is provided.
304+
* Otherwise, terms will be fetched using `wp_get_object_terms()`,
305+
* which respects the default query arguments set for the taxonomy.
306+
*/
307+
if (
308+
empty( $prepared_args['post'] ) &&
309+
isset( $taxonomy_obj->args ) &&
310+
is_array( $taxonomy_obj->args )
311+
) {
312+
$prepared_args = array_merge( $prepared_args, $taxonomy_obj->args );
313+
}
314+
298315
/**
299316
* Filters get_terms() arguments when querying terms via the REST API.
300317
*

tests/phpunit/tests/rest-api/rest-tags-controller.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,55 @@ public function test_get_items_custom_tax_post_args() {
482482
$this->assertSame( 'Cape', $data[0]['name'] );
483483
}
484484

485+
/**
486+
* @ticket 62500
487+
*/
488+
public function test_get_items_custom_tax_without_post_arg_respects_tax_query_args() {
489+
register_taxonomy(
490+
'batman',
491+
'post',
492+
array(
493+
'show_in_rest' => true,
494+
'sort' => true,
495+
'args' => array(
496+
'order' => 'DESC',
497+
'orderby' => 'name',
498+
),
499+
)
500+
);
501+
$controller = new WP_REST_Terms_Controller( 'batman' );
502+
$controller->register_routes();
503+
$term1 = self::factory()->term->create(
504+
array(
505+
'name' => 'Cycle',
506+
'taxonomy' => 'batman',
507+
)
508+
);
509+
$term2 = self::factory()->term->create(
510+
array(
511+
'name' => 'Pod',
512+
'taxonomy' => 'batman',
513+
)
514+
);
515+
$term3 = self::factory()->term->create(
516+
array(
517+
'name' => 'Cave',
518+
'taxonomy' => 'batman',
519+
)
520+
);
521+
522+
$request = new WP_REST_Request( 'GET', '/wp/v2/batman' );
523+
$response = rest_get_server()->dispatch( $request );
524+
$this->assertSame( 200, $response->get_status() );
525+
526+
$data = $response->get_data();
527+
$this->assertCount( 3, $data );
528+
$this->assertSame(
529+
array( 'Pod', 'Cycle', 'Cave' ),
530+
array_column( $data, 'name' )
531+
);
532+
}
533+
485534
public function test_get_items_search_args() {
486535
$tag1 = self::factory()->tag->create( array( 'name' => 'Apple' ) );
487536
$tag2 = self::factory()->tag->create( array( 'name' => 'Banana' ) );

0 commit comments

Comments
 (0)