Skip Navigation

[Resolved] How to update a field with multiple values (repeating field) using PHP

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

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

Problem:
I am trying to save multiple values in a reapating custom field.
How do I update several instances of the repeating field?

Solution:
https://toolset.com/forums/topic/cant-save-array-data-in-one-custom-field/#post-460826

This support ticket is created 7 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by rameshP 7 years, 11 months ago.

Assisted by: Beda.

Author
Posts
#460814
array.png

I am trying to save multiple data in one custom field (numbers). I create the field 'tag-id' choose the option 'Allow multiple-instances of this field'.

I want to save these data using function so I tried this

		$poddata = Array(1,2);
		update_post_meta( $topic_id, 'wpcf-tag-id', $poddata );

If I add single value then it works. But using array it's not working. It shows me 'array' word in the field. I visited this topic - https://toolset.com/forums/topic/gravityforms-checkbox-values-not-passed-to-custom-fields-checkboxes/ - But I didn't understand anything.

can anyone help me with it?

Thank you

#460826

A repeating Types field is stored as this in the Database:

1. One entry each value
2. An entry for the sort order of the repeating values.

Let's say you have one repeating field, with 2 values (value_one and value_two), then in the DDBB you will have:
value_one:
meta_id: 236, post_id: 52, meta_key: wpcf-repeating-field

value_two:
meta_id: 237, post_id: 52, meta_key: wpcf-repeating-field

sort order for above two values:
meta_id: 238, post_id: 52, meta_key: _wpcf-repeating-field-sort-order
(this is a serialized array: a:2:{i:0;i:236;i:1;i:237;})

As you see when you update a Custom Field that is repeating, you need to pass the values and the sort order.
You need to update the single values in a Foreach, and then update the order outside that foreach.

I can not help you to build the given array coming from another plugin, but I could show you how this is done with a repeating Field updating another repeating field.

How to get the Field from the Database:

//We are taking out a Repeating Field (Single Line) of possible items 
//This will give us an Array() of Possible Items of the repeating field
$field_content = get_post_meta($single_post_id, 'wpcf-field-repeating');

//We also need the hidden field for the above repeating Field (_wpcf-field-repeating-sort-order)
$sort_order_repeating = get_post_meta($single_post_id, '_wpcf-field-repeating-sort-order', true);

How to update another Field with above:

//go over all repeating fields values  (possible items)
foreach ($field_content as $field_content_single ) {

//create a new $variable with single Question post Object
$single_value = $field_content_single;

//add/update the repeating possible Items
add_post_meta($new_post_id, 'wpcf-field-repeating', $single_value);
}
//update the order of those repeating fields
update_post_meta($new_post_id, '_wpcf-field-repeating-sort-order', $sort_order_repeating);
#460827

Now I understand it.

Thank You so much for help Beda.

I updated my code to this to save multiple data in one custom field.

		$poddata = array(3,5,4);	
		foreach ($poddata as $field_content_single ) {
		$single_value = $field_content_single;
		add_post_meta($topic_id, 'wpcf-tag-id', $single_value);
		update_post_meta($topic_id, '_wpcf-tag-id-sort-order', $sort_order_repeating);
		}

The forum ‘Types Community Support’ is closed to new topics and replies.