-
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
Taxonomies: Retain flat term order #64471
Conversation
Size Change: +8 B (0%) Total Size: 1.82 MB
ℹ️ View Unchanged
|
958c94f
to
d73d159
Compare
d73d159
to
9859995
Compare
The server-side part that would be needed for this seems to be a bit more complex. I'll file an issue to accompany this PR and will track my findings there. |
There's one additional caveat when it comes to sorting terms in a "natural" order. WooCommerce is adding several taxonomies, e.g., Product Categories, and lets users to sort the terms using drag&drop handles: This order is sorted as an Therefore, the "natural" order is not always by ID, how the terms were chronologically added. Besides Woo, there are also specialized popular plugins that add the same feature to any taxonomy. This Woo feature currently doesn't work with Gutenberg. It probably works when the terms are listed in a classic theme using a PHP function, but i) the REST API doesn't respect this order ii) neither the Gutenberg categories block respects that when rendered by the server for the frontend. Note in the following screenshots that the "Tables" category should be the first, but it isn't: I'm sharing this here because it seems to me that all these problems are closely related and a 100% correct patch will solve them all at once. |
9859995
to
acae7f6
Compare
Flaky tests detected in ef8ef09. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11918419850
|
acae7f6
to
70a8855
Compare
I finally managed to get back to this. I've given this some more thought, and I think one of the key problems here is that setting This is complicated by the fact that high-level functions such as It seems that the recommended way of defining a different sort order is at
For example, (As a corollary, I believe that using This raises the question if I'm torn on this. I've started trying it out in WordPress/wordpress-develop#7287. It's working for the REST API (thus retaining term order in the editor sidebar panel), but not on the frontend, which goes through a different pipeline (via I'd be very curious to hear your thoughts on this @jsnajdr! |
One more interesting find: The
|
I think I have an idea how to proceed.
|
f421545
to
239cee2
Compare
Something doesn't sit quite right with me about querying the sort order from the network only to include in another network request -- it sounds like the server should have all the information it needs, so I'll try to think about this some more. The big question is: If a taxonomy is registered with both |
Getting closer to an answer. Turns out that to get the list of terms, the controller uses either And the queries that (We might still want to consider why |
Ah, we can't really do that. The problem is that when a new term is entered by the user, it is created (via a POST request), but not yet associated with the current post. This is by design: We only want that to happen once the user clicks the 'Save' button in order to persist their changes. (Otherwise, the new term would show up on that post, even if the user ends up never saving the post.) So it looks like we need to explore the other avenue:
|
The following works (for a taxonomy called diff --git a/lib/compat/wordpress-6.8/rest-api.php b/lib/compat/wordpress-6.8/rest-api.php
index da1b657cda0..3c465e2a487 100644
--- a/lib/compat/wordpress-6.8/rest-api.php
+++ b/lib/compat/wordpress-6.8/rest-api.php
@@ -20,3 +20,13 @@ if ( ! function_exists( 'gutenberg_add_post_type_rendering_mode' ) ) {
}
}
add_action( 'rest_api_init', 'gutenberg_add_post_type_rendering_mode' );
+
+function gutenberg_respect_taxonomy_default_args( $args ) {
+ $taxonomy = $args['taxonomy'];
+ $t = get_taxonomy( $taxonomy );
+ if ( isset( $t->args ) && is_array( $t->args ) ) {
+ $args = array_merge( $args, $t->args );
+ }
+ return $args;
+}
+add_filter( 'rest_actors_query', 'gutenberg_respect_taxonomy_default_args' ); Now I'll have to see how I can generalize it 🙂 |
Closing in favor of #67154. |
Yes, Woo stores the custom order in an
Woo doesn't add these default args in |
What?
See #65052.
Why?
To retain the order in which "flat terms" (such as tags) are stored for a given post.
How?
Sort the terms obtained via
getEntityRecords
by the array of term IDs that reflects the order in which they were added by the user to the current post.TODO
sort
setting that's provided toregister_taxonomy
to determine whether terms should be stored in the order they're given by the user (ifsort === true
, somewhat counter-intuitively), or if we should continue to sort them alphabetically.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 the order is kept (unlike previously, when they were re-ordered in alphabetical order). Note however that when saving the post, the terms are still sorted alphabetically, and their previous order is lost. See WordPress/wordpress-develop#7287 for a WIP fix.Screenshots or screencast