[Resuelto] How to save repeating fields with update_post_meta
Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.
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.
This support ticket is created hace 6 años, 12 meses. 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.
Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.
$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.
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:
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.
$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.