I have a taxonomy in which term order matters, but varies from post to post. So I want to be able to set the order that terms are displayed in on a per-post basis:
My first post | Tagged: Apples, Oranges, Bananas
Some other post | Tagged: Oranges, Bananas, Apples
I am looking for advice on how to implement this. Here is an outline of my current thinking:
Per-post term ordering using a custom field
Remove standard taxonomy metabox using
Add custom metabox containing term ordering interface, e.g., drag-and-drop (or simple text input next to each term for an integer describing term's order. (Or perhaps it's possible to add drag-and-drop to the existing meta_box without having to remove and add a new one?**)
Create a custom function
my_get_the_terms()that loops through the IDs in the _taxonomyname_termorder custom field. If a post has terms but no _taxonomyname_termorder field (as with old posts), just return get_the_terms().
In relevant theme files, replace instances of
get_the_terms()or other native term functions with
Do you have advice, alternatives, warnings, or related snippets? I'd be grateful for any thoughts. Thank you!
- What if the list in the custom field gets out of sync with the actual term list (as it would if terms were added through an interface other than the post edit screen, such as the quick-edit/bulk edit form or Front-End Editor).
Solution: At the beginning of
my_get_the_terms(), we can check whether the IDs in the custom field match the terms returned by get_the_terms. If not, default to get_the_terms.
New terms wouldn't be captured on first use, because they don't yet have an ID. You'd have to save a new term before it would work. (I could live with this, but it is a problem.)
The meta box has to list terms in the right order when the post edit page loads.
** For drag-and-drop sorting in the default metabox, I discovered that I can use jQuery UI's .sortable, already available in the admin; for a non-hierarchical taxonomy, it's