5

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 );*/
Community
  • 1
  • 1
vespino
  • 1,714
  • 3
  • 15
  • 28
  • 1
    Please have a look at: http://www.advancedcustomfields.com/resources/update_sub_field/ – radscheit Nov 07 '15 at 21:46
  • 1
    That function only updates existing rows and does not create new ones. – vespino Nov 08 '15 at 21:50
  • 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 hash – Ralph Vugts Nov 25 '15 at 00:08

2 Answers2

2

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' );
Ralph Vugts
  • 425
  • 5
  • 14
2

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 );
John Dorner
  • 1,085
  • 1
  • 8
  • 15