Skip Navigation

[Resolved] How to save repeating fields with update_post_meta

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

Problem: When I save repeating field values with update_post_meta, all the repeating fields get set to the value of the last original repeating field in the database. I'm using the following code:

$fields = get_post_meta($post_id, 'wpcf-field-repeating');
....
some changes to $fields
... 
foreach ($fields as $single_value ) {
  update_post_meta($post_id, 'wpcf-field-repeating', $single_value);

Solution: Be sure to provide the 4th parameter to the update_post_meta function to update a single instance of a repeating field, otherwise all instances will be set to the same value. Example:

$prev_value = (previous value of this instance);
update_post_meta($post_id, 'wpcf-field-repeating', $single_value, $prev_value);

Another option would be to delete all values of this repeating field first, then insert all the new values.

Relevant Documentation: https://codex.wordpress.org/Function_Reference/update_post_meta#Parameters

This support ticket is created 6 years, 11 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 3 voices.

Last updated by Mario 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#594379

I have a repating field "wpcf-field-repeating" (file) and want update existing values it with new values (array) while executing an action-hook.
Unfortunately my update_post_meta doesn´t work for repeating fields. I guess it has to do with sorting/serializing.
i found this for example:
https://toolset.com/documentation/user-guides/repeating-fields/
https://toolset.com/forums/topic/cant-save-array-data-in-one-custom-field/#post-460826

$fields =get_post_meta($post_id, 'wpcf-field-repeating');
....
so changes to $fields
...
$sort_order=get_post_meta($post_id, '_wpcf-field-repeating-sort-order', true);

   foreach ($fields as $single_value ) {
       update_post_meta($post_id, 'wpcf-field-repeating', $single_value);
    }
update_post_meta ($post_id, '_wpcf-field-repeating-sort-order', $sort_order);

$sort_order is empty and it wouldn´t work for additional values. So question is where could i find a working code-snippet for my objective to store this file repeating field?

Am i right, that in upcoming releases of types plugin (relationships) the repeating fields will change? Especially how they are stored in database?
Thanks for any hints.

#594489

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mario,

Thank you for contacting our support forum.

First we need to do some debugging as to why this not working.

Lets check whats in the $fields variable by doing this.

var_dump($fields);

Please add that to your code and let me know what it returns.

Thanks
Shane

#594709

$sort_order is empty and finally just last value inserted always in DB.

$fields =get_post_meta($post_id, 'wpcf-field-repeating');
log($fields);

$sort_order=get_post_meta($post_id, '_wpcf-field-repeating-sort-order', true);
log("sort_order=" . $sort_order);
 
   foreach ($fields as $single_value ) {
      update_post_meta($post_id, 'wpcf-field-repeating', $single_value);
      log($single_value);
   }
update_post_meta ($post_id, '_wpcf-field-repeating-sort-order', $sort_order);
$fields =get_post_meta($post_id, 'wpcf-field-repeating');
log($fields);
30.11. 12:17:17 Array
(
    [0] => <em><u>hidden link</u></em>.../wp-content/uploads/ob-files/112-0-2017-09-07_11h25_51.png 
    [1] => <em><u>hidden link</u></em>.../wp-content/uploads/ob-files/112-1-presentation.pptx 
)
30.11. 12:17:17 sort_order=

30.11. 12:17:17 <em><u>hidden link</u></em>.../wp-content/uploads/ob-files/112-0-2017-09-07_11h25_51.png 
30.11. 12:17:17 <em><u>hidden link</u></em>.../wp-content/uploads/ob-files/112-1-presentation.pptx 

30.11. 12:17:17 Array
(
    [0] => <em><u>hidden link</u></em>.../wp-content/uploads/ob-files/112-1-presentation.pptx 
    [1] => <em><u>hidden link</u></em>.../wp-content/uploads/ob-files/112-1-presentation.pptx 
)
#594887

Hi, Shane is out for a week on vacation, so I've been asked to take a look at this ticket. I hope that's okay with you.

$sort_order is empty
Right, because you do not modify the value in your code. You get the original value and then set the same value without changing it:

$sort_order=get_post_meta($post_id, '_wpcf-field-repeating-sort-order', true);
// ...
// ...
update_post_meta ($post_id, '_wpcf-field-repeating-sort-order', $sort_order);

The $sort_order variable never changes. So if the original value was empty, the new value will also be empty. Please let me know if I'm misunderstanding what you want to accomplish here, but it appears that sort order will never be set to anything different than its previous value with this code.

and finally just last value inserted always in DB.
This is the expected behavior of update_post_meta when only 3 parameters are provided. Please refer to the documentation for this function's parameters here:
https://codex.wordpress.org/Function_Reference/update_post_meta#Parameters

$prev_value
(mixed) (optional) The old value of the custom field you wish to change. This is to differentiate between several fields with the same key. If omitted, and there are multiple rows for this post and meta key, all meta values will be updated.
Default: Empty

In order to update each repeated value independently, you must include the 4th parameter "prev_value" to specify which of the repeating values you want to update. Otherwise, all values will be updated using the same value.

#595071

OMG...thank you so much Christian. So simple, missed it totaly.