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.
Built from https://develop.svn.wordpress.org/trunk@59458


git-svn-id: http://core.svn.wordpress.org/trunk@58844 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Bernhard Reiter 2024-11-25 11:16:22 +00:00
parent 8f303471a4
commit f03be09041
2 changed files with 18 additions and 1 deletions

View File

@ -227,6 +227,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
* Retrieves terms associated with a taxonomy. * Retrieves terms associated with a taxonomy.
* *
* @since 4.7.0 * @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. * @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. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
@ -295,6 +296,22 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
} }
} }
/*
* 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. * Filters get_terms() arguments when querying terms via the REST API.
* *

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.8-alpha-59457'; $wp_version = '6.8-alpha-59458';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.