Skip to content

Commit

Permalink
REST API: Terms: Respect taxonomy's default query args.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ockham committed Nov 25, 2024
1 parent 27c566f commit 1aa41de
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public function get_items_permissions_check( $request ) {
* Retrieves terms associated with a taxonomy.
*
* @since 4.7.0
* @since 6.8.0 Respect default query arguments set for the taxonomy upon registration.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
Expand Down Expand Up @@ -295,6 +296,22 @@ public function get_items( $request ) {
}
}

/*
* When a taxonomy is registered with an 'args' array,
* those params override the `$args` passed to this function.
*
* We only need to do this if no `post` argument is provided.
* Otherwise, terms will be fetched using `wp_get_object_terms()`,
* which respects the default query arguments set for the taxonomy.
*/
if (
empty( $prepared_args['post'] ) &&
isset( $taxonomy_obj->args ) &&
is_array( $taxonomy_obj->args )
) {
$prepared_args = array_merge( $prepared_args, $taxonomy_obj->args );
}

/**
* Filters get_terms() arguments when querying terms via the REST API.
*
Expand Down
49 changes: 49 additions & 0 deletions tests/phpunit/tests/rest-api/rest-tags-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,55 @@ public function test_get_items_custom_tax_post_args() {
$this->assertSame( 'Cape', $data[0]['name'] );
}

/**
* @ticket 62500
*/
public function test_get_items_custom_tax_without_post_arg_respects_tax_query_args() {
register_taxonomy(
'batman',
'post',
array(
'show_in_rest' => true,
'sort' => true,
'args' => array(
'order' => 'DESC',
'orderby' => 'name',
),
)
);
$controller = new WP_REST_Terms_Controller( 'batman' );
$controller->register_routes();
$term1 = self::factory()->term->create(
array(
'name' => 'Cycle',
'taxonomy' => 'batman',
)
);
$term2 = self::factory()->term->create(
array(
'name' => 'Pod',
'taxonomy' => 'batman',
)
);
$term3 = self::factory()->term->create(
array(
'name' => 'Cave',
'taxonomy' => 'batman',
)
);

$request = new WP_REST_Request( 'GET', '/wp/v2/batman' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );

$data = $response->get_data();
$this->assertCount( 3, $data );
$this->assertSame(
array( 'Pod', 'Cycle', 'Cave' ),
array_column( $data, 'name' )
);
}

public function test_get_items_search_args() {
$tag1 = self::factory()->tag->create( array( 'name' => 'Apple' ) );
$tag2 = self::factory()->tag->create( array( 'name' => 'Banana' ) );
Expand Down

0 comments on commit 1aa41de

Please sign in to comment.