5
votes

I'm looking for a way to add rows to a repeater field in ACF Pro. I found this post but the solution post doesn't seem to work. I will describe my problem:

I have a custom post type called "gebruikers", a repeater field called "logins" and a rows that can have a field called "datum". I would like to be able to add a value to a new row in the field "datum". Is this possible?

My code so far:

$field_key = "logins";
$user_id = "gebruiker_23";
$value = get_field($field_key, $user_id);
$value[] = array('date' => date('Y-m-d H:i:s'));
update_field( $field_key, $value, $user_id );*/
2
That function only updates existing rows and does not create new ones.vespino
I'm having the same issue - your example works if you have added a row to the reapeater... but if it has not data at all it doesn't create the field hashRalph Vugts

2 Answers

2
votes

Finally figured this out!

Change $field_key = "logins"; to the field key that is in the Wordpress posts table. Grab the field_id from there (post_name column) that is assigned to your main repeater field.

Use the ID from post_name.

Now you can insert to these records if none already exist.

Example that worked for me on a user record with no previous rows:

$field_key = "field_5657dffe586cc";
$value = get_field($field_key, 'user_24');
$value[] = array("career_enjoyment_id" => "Foo", "career_enjoyment_file" => "Bar");
update_field( $field_key, $value, 'user_24' );
2
votes

I think the add_row function is what you are looking for. https://www.advancedcustomfields.com/resources/add_row/

$row = array(
 'datum' => 'new value',
); 
$i = add_row( 'logins', $row );