Skip to content

Commit

Permalink
Terms: Respect order specified by register_taxonomy() (#67154)
Browse files Browse the repository at this point in the history
It is possible to supply a default set of `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. This commit makes it so that the list of terms returned by the Terms REST API controller respects that order.

Co-authored-by: ockham <[email protected]>
Co-authored-by: jsnajdr <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2024
1 parent 21ee975 commit a6a04be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.8/7848.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/7848

* https://github.com/WordPress/gutenberg/pull/67154
29 changes: 29 additions & 0 deletions lib/compat/wordpress-6.8/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,32 @@ function gutenberg_add_post_type_rendering_mode() {
}
}
add_action( 'rest_api_init', 'gutenberg_add_post_type_rendering_mode' );

// When querying terms for a given taxonomy in the REST API, respect the default
// query arguments set for that taxonomy upon registration.
function gutenberg_respect_taxonomy_default_args_in_rest_api( $args ) {
// If a `post` argument is provided, the Terms controller will use
// `wp_get_object_terms`, which respects the default query arguments,
// so we don't need to do anything.
if ( ! empty( $args['post'] ) ) {
return $args;
}

$t = get_taxonomy( $args['taxonomy'] );
if ( isset( $t->args ) && is_array( $t->args ) ) {
$args = array_merge( $args, $t->args );
}
return $args;
}
add_action(
'registered_taxonomy',
function ( $taxonomy ) {
add_filter( "rest_{$taxonomy}_query", 'gutenberg_respect_taxonomy_default_args_in_rest_api' );
}
);
add_action(
'unregistered_taxonomy',
function ( $taxonomy ) {
remove_filter( "rest_{$taxonomy}_query", 'gutenberg_respect_taxonomy_default_args_in_rest_api' );
}
);

0 comments on commit a6a04be

Please sign in to comment.