Support Forum
Hi, i need your help.
First of all, i got my taxonomy sort option to true which make the terms being ordered and saved on wp_term_relationships->term_order.
$wp_taxonomies['post_tag']->sort = true;
it's all good, until i want to update my post that already being published and wordpress show and order the terms based on first letter on the editor screen so when i click "update", all the terms now lose the order that i've set before. well, i can re-order the terms manually each time i wanna update the post but that's really not efficient especially when i have lot of terms.
i want to prevent taxonomy order being overwritten when i updated a post. maybe something like
if post status is published, prevent/disable terms being saved to database
Thanks
Hi,
Can you please tell me how do you set term order for a post?
When I go to WP database, I see the term_order
is available only for term_relationships
table, which stores the connections between posts and terms. And the sooner a term in inserted, the lower order it has.
When we update a post, Meta Box runs wp_set_object_terms()
function to update post terms. This function does these things:
term_order
)The last step causes the problem you have met. You can check the source code of wp_set_object_terms()
function to see more details. Here is the screenshot of that part:
https://i.imgur.com/YeYlGhc.png
The bad thing is this is the only function we can use to set post terms :(.
Maybe there's another approach to set the order? Have you tried taxonomy_advanced
field?
i simply set this option https://imgur.com/vhZR41A
and it will set this parameter on taxonomy setting:
sort' => true,
and yes, the term order is saved on term_order column
inside term_relationships table
. to set the term order, you just need to insert it on taxonomy field like you normally do, but the difference is input sequence is matter. so you just need to insert the terms in sequence and the order set in sequence.
it's an abandoned feature on wordpress and people didnt pay attention much to it, you can set the order but to show it you still need to use your custom query.
that's what im doing right now but having a trouble like i mention above. i think it's a waste to try another solution, because wordress already provide the column for us and we just need to step up using our own feet to use it.
I understand.
There's a technical difficulty. The select2
, which is the library used for taxonomy field, doesn't support preserving selected order. I'm trying some workarounds posted on that issue to see if I can make it work in WordPress's case.
i activate this hook so i can modify wordpress post_tag sort parameter to true:
function postTag_sort_tags() {
global $wp_taxonomies;
$wp_taxonomies['post_tag']->sort = true;
}
add_action( 'init', 'postTag_sort_tags' );
do you have any idea how to set it back to false after checking some condition? i've tried it but it seems init hooks is too early to get the post ID which in this case i need it for the condition.
Hi again,
I think I have resolved this issue. I've just pushed a commit on Github to make it work. Can you please try it.
This is the code I use to test:
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
$meta_boxes[] = [
'title' => 'Test: Taxonomy term order',
'fields' => [
[
'id' => 'm_tax',
'type' => 'taxonomy',
'taxonomy' => 'custom-category',
'field_type' => 'select_advanced',
'multiple' => true,
],
],
];
return $meta_boxes;
} );
Note that multiple
is set to true
. It's required to select multiple terms.
Ah i'll take a look at it and give you an update about it later, thank you!
Hi Anh,
I can see my terms in order on the select advanced field, but when i wanted to change the order, it does nothing. the order is still the same. when i add a new term, it get pushed to the very first order of the terms even though i put in the middle. I'm using wordpress default post_tag taxonomy and the default Tags field still showing on my editor screen. Do i miss something?
Hi,
I'm not sure if the select advanced library support changing order of selected items. AFAIK it doesn't. That's why I was curious about how did you set the order for the terms in the previous reply.
The current workaround pushes the selected items to the bottom of the list and un-selected items to the top. It's kind of the unfriendly experience for users, but that's the only way to preserve the order of selected items. (You can see the details in the Github link I posted above. It's worth noting that this is a temporary solution, as it's NOT supported by the select2 library).
By the way, can you post the code you use for post_tag with Meta Box?
Currently I'm using wordpress default Tags taxonomy, it's nothing to do with metabox. here's the code:
function my_sort_tags() {
global $wp_taxonomies;
$wp_taxonomies['post_tag']->sort = true;
}
add_action( 'init', 'my_sort_tags' );
Then if i insert my terms on default wordpress taxonomy fields, it will remember the order when saving it to the database but not showing it in order when retrieve it from database to be showed on the editor screen so i have to manually re-order the terms if i ever want to update post.
idk Anh, but i think i'm gonna give on this for now and back to my old method, re-order the terms manually when post is updated. Thanks!
Ooops, I was thinking you're using the taxonomy
field from Meta Box. If you're using the WordPress's core meta box for post tag, then it's out of the scope of the plugin. Maybe you should open a ticket for WordPress team?
Yes, I'm sorry if i dont make it clear enough for you. i thought i can achieve it using meta box taxonomy fields because on MB Custom Taxonomy, user allowed to set the sort parameter when creating custom taxonomy so i thoght it will integrate itself with meta box taxonomy fields when i set it to true.
No problem. The MB Custom Taxonomy only set the taxonomy parameters. Other things are handled by WordPress itself.
I think we can close this topic :). Have a nice day!