Opened 4 years ago
Last modified 7 days ago
#9547 reopened feature request
Taxonomy - interesting 'unused' term_order column in table term_relationships.
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | high | Milestone: | Future Release |
| Component: | Taxonomy | Version: | 2.8 |
| Severity: | normal | Keywords: | has-patch needs-unit-tests |
| Cc: | kevinB, luke.gedeon@…, lkraav, simon@…, ben@…, tollmanz@…, wordpress@… |
Description
During development of plugin xili-language, and to sort term by term list of languages in a taxonomy, I discover unused column term_order in term_relationships table and lack of functions in core about this column. Like medias in post, here the user can define languages list with first, second, third,... languages for his website (and xml header). Taxonomy tools are here very powerful without adding tables or annoying coding.
(see code here line 1309-1370).
Before to complete these very basic functions,…
Is it forecast to have more basic / generic functions using term_order in taxonomy.php ?
Attachments (6)
Change History (29)
- Milestone changed from Unassigned to 2.9
- Version set to 2.8
there are several duplicates of this bug around. most were closed as wontfix, on grounds of all sorts of fishy looking reasons. to find them, search for page order, or category order.
- Milestone changed from 2.9 to 2.8
- Resolution set to fixed
- Status changed from new to closed
please re-open if r11250 isn't enough.
- Milestone changed from 2.8 to Future Release
- Resolution fixed deleted
- Status changed from closed to reopened
sorry, mixed up with a separate ticket I remembered about.
- Cc luke.gedeon@… added
I propose adding an object_order column for this purpose. Term_order was intended for prioritizing multiple terms as applied to a single object. (see #5857)
Object_order would be a logical place for storing the order in the opposite direction - multiple objects assigned to the same term. Both are very helpful concepts.
I will be attempting to write functions for the term_order concept to submit as a patch. If the object_order concept is blessed, you may be able to use some of my code for that purpose too.
No patch needed, actually. Here is the code to use term_order to sort tags.
/**
* Sort post_tags by term_order
*
* @param array $terms array of objects to be replaced with sorted list
* @param integer $id post id
* @param string $taxonomy only 'post_tag' is changed.
* @return array of objects
*/
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;
$terms = wp_cache_get($id, "{$taxonomy}_relationships_sorted");
if ( false === $terms ) {
$terms = wp_get_object_terms( $id, $taxonomy, array( 'orderby' => 'term_order' ) );
wp_cache_add($id, $terms, $taxonomy . '_relationships_sorted');
}
return $terms;
}
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' );
?>
To use this, you will need to add tags in the order you want them to be and if you get them out of order you may have to remove some and add them back correctly. I might write a better UI later, but for now this at least works.
I guess next actions would be to write a patch for object ordering, and to come up with a UI(s) for both tag-style and category-style term_ordering.
comment:8
jeremyfelt — 15 months ago
Closed #20200 as duplicate.
comment:10
follow-ups:
↓ 12
↓ 14
simonwheatley — 10 months ago
- Cc simon@… added
- Keywords has-patch dev-feedback added
I've created a patch which adds the ability to request terms ordered by term_order when using get_the_terms or get_the_term_list. Points to note:
- I've added “simple” function arguments, rather than using an $args style array as a final parameter as this seems to be the pattern for things nearer template tags than API functions, happy to change the patch if this isn't the right approach.
- This has affected the caching in get_the_terms in that it no longer simply uses the cache controlled by get_object_term_cache, because this cache does not respect ordering. It is not currently possible to pull the terms from the cache and order in PHP within the get_the_terms function, as the term objects do not contain the term_order property, even if it was deemed appropriate to sort terms in PHP at this point. I'd argue that the cache will still fill, even if there's more keys in it, and when it's full the system is just as cached as it was before, i.e. we've still got the function covered by a cache.
comment:11
simonwheatley — 10 months ago
Almost forgot. I'll attach a proof of concept plugin which converts post_tag into a sorted taxonomy…
comment:12
in reply to:
↑ 10
westi — 10 months ago
Replying to simonwheatley:
I've created a patch which adds the ability to request terms ordered by term_order when using get_the_terms or get_the_term_list. Points to note:
- I've added “simple” function arguments, rather than using an $args style array as a final parameter as this seems to be the pattern for things nearer template tags than API functions, happy to change the patch if this isn't the right approach.
- This has affected the caching in get_the_terms in that it no longer simply uses the cache controlled by get_object_term_cache, because this cache does not respect ordering. It is not currently possible to pull the terms from the cache and order in PHP within the get_the_terms function, as the term objects do not contain the term_order property, even if it was deemed appropriate to sort terms in PHP at this point. I'd argue that the cache will still fill, even if there's more keys in it, and when it's full the system is just as cached as it was before, i.e. we've still got the function covered by a cache.
I've taken a quick look and it looks good.
Do you fancy creating some unit tests for this new functionality too?
comment:13
simonwheatley — 10 months ago
Sure. I may need some guidance. :)
/me Reads http://make.wordpress.org/core/handbook/automated-testing/
comment:14
in reply to:
↑ 10
SergeyBiryukov — 10 months ago
- Keywords taxonomy term_order relationships removed
Replying to simonwheatley:
I've added “simple” function arguments, rather than using an $args style array as a final parameter as this seems to be the pattern for things nearer template tags than API functions, happy to change the patch if this isn't the right approach.
#14634 suggests adding $args.
simonwheatley — 10 months ago
Add ordering to term template tags through $args array type param (removed error_log call)
comment:15
simonwheatley — 10 months ago
Replying to SergeyBiryukov:
#14634 suggests adding $args.
Thanks Sergey. I can see this being more flexible, though it does increase complexity and I wasn't sure if this kind of parameter was in the spirit of the WordPress template tags. I've added it as a separate patch to make things easier to compare (diffing diffs… fun).
#9547-sw2.diff demonstratesget_the_terms and get_the_term_list with the $args parameter.
A further caching caveat with this patch: If you pass an empty array in $args (as is the default) it will cache under a different key to a call where the $args param contains values equivalent to the defaults in wp_get_object_terms. This is because the patch is MD5 hashing the serialised $args array. I can't think of a way around this without duplicating the defaults from wp_get_object_terms in get_the_terms (and I suspect in most cases the problem is not serious).
comment:16
follow-up:
↓ 17
SergeyBiryukov — 10 months ago
#9547-sw2.diff has a var_dump() left in line 1077 and still passes two separate arguments to get_the_term_list() in the_terms().
simonwheatley — 10 months ago
Add ordering to term template tags through $args array type param (removed error_log call, var_dump, corrected the_terms args param)
comment:17
in reply to:
↑ 16
simonwheatley — 10 months ago
Replying to SergeyBiryukov:
#9547-sw2.diff has a var_dump() left in line 1077 and still passes two separate arguments to get_the_term_list() in the_terms().
Thanks for spotting the the_terms param missing, apologies for the var_dump. Attaching a new diff.
wonderboymusic — 4 months ago
comment:18
wonderboymusic — 4 months ago
- Keywords needs-unit-tests added; dev-feedback removed
- Milestone changed from Future Release to 3.6
Patch no longer applied cleanly, updated and changed the cache key / group to be more friendly with others
comment:19
SergeyBiryukov — 3 months ago
#14634 was marked as a duplicate.
comment:20
in reply to:
↑ 6
husobj — 8 weeks ago
- Cc ben@… added
Replying to lgedeon:
I propose adding an object_order column for this purpose. Term_order was intended for prioritizing multiple terms as applied to a single object. (see #5857)
Object_order would be a logical place for storing the order in the opposite direction - multiple objects assigned to the same term. Both are very helpful concepts.
If term_order is intended to be used to prioritise the order of terms associated with posts, I second the idea of a object_order column for prioritising the order of posts within terms - essentially the equivalent of menu_order for posts, but for taxonomies.
comment:21
tollmanz — 3 weeks ago
- Cc tollmanz@… added
Added unit tests that attempt to test the latest patch as well as do a few other things:
- Ordering using wp_get_post_terms is tested. The latest patch adds the ability to order by "term_group" and previously no ordering has been tested with wp_get_post_terms. I added those tests.
- get_terms, get_the_term_list, and the_terms are tested when the $args parameter is used, which was added with the latest patch for this ticket. In all of these tests, I tested to make sure that the cache key was handled correctly so that different $args values would generate different cache objects.
- The latest patch broke the "test_object_term_cache" test. This has been fixed. The issue was due to the change of the cache key in the latest patch.
comment:22
ryan — 7 days ago
- Milestone changed from 3.6 to Future Release
comment:23
emzo — 7 days ago
- Cc wordpress@… added

Right now galleries are the only user of term_order. No one is currently working on a generic API for using term_order. A patch adding some API would be welcome though.