Prevent terms order being overwritten when updating post
- This topic has 12 replies, 2 voices, and was last updated 7 years ago by
Anh Tran.
-
AuthorPosts
-
April 19, 2018 at 10:18 PM #9287
foundbutlost
ParticipantHi, 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
April 20, 2018 at 3:46 PM #9295Anh Tran
KeymasterHi,
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 forterm_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:- Insert new terms relationships for the post
- Delete old terms relationships
- Reset the relationships order (
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?April 20, 2018 at 5:12 PM #9298foundbutlost
Participanti 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
insideterm_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.
April 23, 2018 at 11:58 AM #9312Anh Tran
KeymasterI 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.April 23, 2018 at 12:22 PM #9313foundbutlost
Participanti 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.
April 23, 2018 at 12:46 PM #9315Anh Tran
KeymasterHi 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 totrue
. It's required to select multiple terms.April 23, 2018 at 6:12 PM #9322foundbutlost
ParticipantAh i'll take a look at it and give you an update about it later, thank you!
April 24, 2018 at 9:19 AM #9339foundbutlost
ParticipantHi 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?
April 24, 2018 at 9:37 AM #9340Anh Tran
KeymasterHi,
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?
April 24, 2018 at 9:50 AM #9342foundbutlost
ParticipantCurrently 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!
April 24, 2018 at 10:18 AM #9344Anh Tran
KeymasterOoops, 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?April 24, 2018 at 10:59 AM #9345foundbutlost
ParticipantYes, 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.
April 24, 2018 at 11:22 AM #9346Anh Tran
KeymasterNo 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!
-
AuthorPosts
- The topic ‘Prevent terms order being overwritten when updating post’ is closed to new replies.