Skip Navigation

[Resolved] How to programmatically add another value of a repeating custom field?

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

Problem:
Posts have a repeating custom field. How in PHP can you add additional instances of the field?

Solution:
Repeating fields are simply stored as additional rows with the same key for the field slug and their own value, so you can simply use update_post_meta to insert new rows.

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

This support ticket is created 6 years 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by Adrian 6 years ago.

Assisted by: Nigel.

Author
Posts
#627548

Hi,

I have a custom function which is supposed to:

1. Add an $ID to a repeating numeric field

and

2. Decrease the "wpcf_remaining" numeric field by 1.

I was able to access the fields and map them to php variables already, I know how to read and display them, but I need to update the values.

Thank you

#627688

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2018-03-23 at 09.54.50.png

Hi Adrian

What is the issue specifically.

I'm sure you understand updating post meta, and you must have looked in the wp_postmeta table and seen that repeating fields are simply additional rows with the same key for the field slug with their own value (see screenshot).

Is your question about targeting specific rows, e.g. if there are 4 repetitions of a field and you want to update the value of a particular one?

If you look at the documentation for update_post_meta you will see that there is an optional $prev_value parameter used to target specific instances to be updated: https://codex.wordpress.org/Function_Reference/update_post_meta#Usage

#627821

not sure I understand.

I basically want to add a value to the repeating field `wpcf-unlocked-ids` using PHP. I don't care what values are there, I don't need to change any of them or remove any of them, just want to add one to the end.

from what I see here, update post meta will change an existing entry but I need to add entries to the end of that list.
what I was looking for was add_post_meta.

I guess you just add another meta value on top of it and keep going... Wasn't expecting it to be this easy.

The solution is:

add_post_meta( $target_post_ID, 'wpcf-unlocked-ids', $the_id_i_want_to_add );

//$target_post_ID - the ID of the post that has the repeating field
//$the_id_i_want_to_add - the ID of the post I want to reference in here.

Thank you Nigel for pointing me in the right direction.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.