Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress: ACF add rows to a repeater field

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 );*/
like image 438
vespino Avatar asked Sep 26 '22 07:09

vespino


2 Answers

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' );
like image 148
Ralph Vugts Avatar answered Oct 11 '22 13:10

Ralph Vugts


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 );
like image 37
John Dorner Avatar answered Oct 11 '22 14:10

John Dorner