From f03be09041d6a06f6fcb5a38fdbb8721c2ee80e1 Mon Sep 17 00:00:00 2001 From: Bernhard Reiter Date: Mon, 25 Nov 2024 11:16:22 +0000 Subject: [PATCH] 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 --- .../class-wp-rest-terms-controller.php | 17 +++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php index 78c9d1ca2b..455c83a68d 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php @@ -227,6 +227,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller { * 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. @@ -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. * diff --git a/wp-includes/version.php b/wp-includes/version.php index c12c644cb1..feebff78b9 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @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.