Skip Navigation

[Resolved] Updating sort order for fields with multiple values

This thread is resolved. Here is a description of the problem and solution.

Problem:

Setup Repeating Fields sort order using PHP codes.

Solution:

Toolset Types plugin is using a custom field to store the sort order of Repeating Fields, the field slug is "_wpcf-{field-slug}-sort-order", and field value is in serialized array format.

For example Repeating Fields "start-date", the sort order field slug is "_wpcf-start-date-sort-order", and field value is:

a:3:{i:0;i:648;i:1;i:649;i:2;i:650;}

Above numbers 648, 649 and 650 are "meta_id" values of each instance, you can get them with add_post_meta() function of your PHP codes, see WP document:
https://developer.wordpress.org/reference/functions/add_post_meta/#return
Return #Return
(int|false) Meta ID on success, false on failure.

Then update the sort order field value with function update_post_meta().

Then update the sort order field value with function update_post_meta().

Relevant Documentation:

https://developer.wordpress.org/reference/functions/add_post_meta/#return

https://developer.wordpress.org/reference/functions/update_post_meta/

This support ticket is created 4 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by tims-9 4 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1651039

Tell us what you are trying to do?
I am trying to update the sort order for fields that allow multiple values

Is there any documentation that you are following?
https://toolset.com/documentation/customizing-sites-using-php/displaying-repeating-fields-one-kind/
Is there a similar example that we can see? No

What is the link to your site?
hidden link

#1651177

Hello,

Please elaborate the question with more details:
How and where do you want to update the Repeating Fields sort order?

You can simply use drag-and-drop to rearrange them, see our document:
https://toolset.com/documentation/user-guides/custom-content/repeating-fields/#rearranging-multiple-images
section "Rearranging multiple images"

#1651185

hi,
i am saving a form and then updating repeating fields in a relationship intermediary post. the following fields are all repeating fields. how do i then update the sort order meta? Or do i need to?

add_post_meta( $intermediary_id, 'wpcf-start-date', $mystart);
add_post_meta( $intermediary_id, 'wpcf-stop-date', $mystop);
add_post_meta( $intermediary_id, 'wpcf-contract-note', $mycomments);

#1651205

Toolset Types plugin is using a custom field to store the sort order of Repeating Fields, the field slug is "_wpcf-{field-slug}-sort-order", and field value is in serialized array format,
For example Repeating Fields "start-date", the sort order field slug is "_wpcf-start-date-sort-order", and field value is:

a:3:{i:0;i:648;i:1;i:649;i:2;i:650;}

Above numbers 648, 649 and 650 are "meta_id" values of each instance, you can get them with add_post_meta() function of your PHP codes, see WP document:
https://developer.wordpress.org/reference/functions/add_post_meta/#return
Return #Return
(int|false) Meta ID on success, false on failure.

Then update the sort order field value with function update_post_meta():
https://developer.wordpress.org/reference/functions/update_post_meta/

#1651245

My issue is resolved now. Thank you!

#1653581
update_post_meta($intermediary_id,'_wpcf-start-date-sort-order',$myresult );

when i try to update the sort with a:1:{i:0;i:22246;}

i get s:18:"a:1:{i:0;i:22346;}";
Any reason why?

#1653591

How do you setup the var $myresult?
You can setup it as a PHP array, then save it in database, for example:

$myresult = array(123, 456, 789);
update_post_meta($intermediary_id,'_wpcf-start-date-sort-order',$myresult );
#1653629

My issue is resolved now. Thank you!
i didnt realise update_post_meta would automatically serialize the array.

Thanks