-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Terms: Respect order specified by register_taxonomy() #67154
Conversation
82608e6
to
1d6db39
Compare
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
69972b0
to
5e22ffa
Compare
Flaky tests detected in b525718. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11953506179
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is a very nice patch 👍 And the Core backport in WordPress/wordpress-develop#7848 also makes perfect sense. The REST controller should apply the taxonomy's default args.
My answer is yes and no 🙂 I found the original ticket where function plugin_get_the_ordered_terms ( $terms, $id, $taxonomy ) {
if ( 'post_tag' != $taxonomy ) // only ordering tags for now but could add other taxonomies here.
return $terms;
return wp_get_object_terms( $id, $taxonomy, array( 'orderby' => 'term_order' ) );
}
add_filter( 'get_the_terms', 'plugin_get_the_ordered_terms' , 10, 4 );
/* Adds sorting by term_order to post_tag by doing a partial register replacing the default */
function plugin_register_sorted_post_tag () {
register_taxonomy( 'post_tag', 'post', array( 'sort' => true, 'args' => array( 'orderby' => 'term_order' ) ) );
}
add_action( 'init', 'plugin_register_sorted_post_tag' ); It adds
The biggest problem I see here is that ordering by The But So, adding |
Fixes #65052.
Supersedes #64471.
What?
It is possible to supply a default set of
args
toregister_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 PR makes it so that the list of terms returned by the Terms REST API controller respects that order.Why?
To reflect the order in which "flat terms" (such as tags) are supposed to be sorted. For example, see the
actors
taxonomy in the code sample (and screencasts) below: It usesarray( 'orderby' => 'term_order' )
to make sure that terms -- i.e. actor names -- are sorted in the order that they are stored (rather than in alphabetical order).How?
By adding a filter to the
rest_{$this->taxonomy}_query
hook for all registered taxonomies, which then merges anyargs
registered for a given taxonomy into the arguments given by the network request.Note that this mimics behavior already present in
wp_get_object_terms()
. In the Terms Controller, the latter function is used for requests that include a post ID. However, we cannot provide a post ID for reasons first explained here:Instead, our network requests go through a code branch in the controller that uses
get_terms()
(rather thanwp_get_object_terms()
) to query terms. To make that function respect the default args registered for a given taxonomy, we use therest_{$this->taxonomy}_query
hook.Testing Instructions
Add the following code (e.g. to your theme's
functions.php
) and use the "Actors" panel in the block inspector to assign a number of actor names to a given post. Note that their order is kept (unlike previously, when they were re-ordered in alphabetical order). Save the post, and verify that the term order is still kept. Reload the post, and verify that the order still persists.Finally, verify that tags entered into the "Tags" panel are still sorted alphabetically across the same set of operations (tag creation, saving the post, reloading it). This demonstrates that the input field truly reflects the sort order that a taxonomy is registered with.
Screenshots or screencast