I have a Repeater Value which contains a Post Object as sub field 1 and a Number as sub field 2
I’m trying to create a repeater entry (new row) by code in a specific post (ID: 2933). I want my first row to relate post 279 with a Number 1 as value
update_sub_field(array('field_5422339bd5eab', 1, 'field_543afcf561f95'), 279,'2933'); update_sub_field(array('field_5422339bd5eab', 1, 'field_543afcae61f94'), 1,'2933');
I also tried leaving the field values like ‘1’ and ‘279’, also I tried changing the field keys by their field names, and changing ‘2933’ to 2933 but nothing working.
What could be failing?
Edit: I notice this works if the post already has a Repeater row. How can I get it work if the post don’t have any row yet?
Not sure if this help someone:
In my case “update_sub_field()” fails to update POST_META table:
It creates an additional record for the post without repeater name:
and does’nt update the good records:
And from them it updates the wrong ones, “update_sub_field()” seems like the function fails to “add” repeater name to the meta_key, anyway I found a workaround:
Use the core function “update_post_meta()” :
update_post_meta( $post_id, “repeaterName_0_subfield”, “new value” );
update_post_meta( $post_id, “repeaterName_1_subfield”, “new value” );
update_post_meta( $post_id, “repeaterName_2_subfield”, “new value” );
This function update values in custom fields in Post_Meta table.
PD: If you need to know how many “rows” has your repeater you can use this:
$number_rows = get_post_meta( $post_id, “repeaterName” );
This works for me perfectly, I spend a lot of time struggling with update_sub_fiel(), hope there will be an update it soon.
facing the exact same problem.
but cant use your update_post_meta solution because its a repeater in a repeater where i have to add a new row.
i looked up in the database but the meta_key from the second repeater is = subrepeater_ and not repeaterName_0_subrepeater_0_subsubfield.
so how can i refer to the right row in the repeater and add a field in the subrepeater?
The issue here is that update_sub_field does exactly what it says. It updates an existing sub field but does not have the ability to create a new one.
Have a look at my sample code here for a solution:
The topic ‘update_sub_field not working’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!