I have been through the ACF and ACF Repeater Field documentation but it has left me a little bit confused.
I have a bunch of users and I have (through ACF) attached a repeater field to each of them (called Events), the repeater field has a sub-field called Event ID (event_id).
Now the dilemma I have is that I want to be able to add and remove rows of event_id from the user depending on which events they have tried to add and remove.
I know of the update_field($field_key, $value, $post_id) function but I'm not too sure how to utilise it to add rows into sub-fields of a repeater field attached to a user. I also need some direction as to how to remove items.
Any help is appreciated!
To add rows into sub-fields of a repeater field, you can do:
$field_key = "repeater_field";
$user_id = "user_123"; // save to user (user id = 123)
$value = get_field($field_key, $user_id);
$value[] = array("event_id " => 25);
$value[] = array("event_id " => 30);
update_field( $field_key, $value, $user_id );
To remove items, I'd use a PHP function such as the one in the accepted answer at Delete element from multidimensional-array based on value:
$value = removeElementWithValue($value, "event_id", 25);
update_field( $field_key, $value, $user_id );
As to where to add these codes (ie. the action hook) depends on when you'd like to add/remove the items.
Just some addition to Angelie Macalansag answer which is good.
This is for wordpress version <5 , since new version have add_row() function that solve problem.
Sad part is $field_key is an problem. Its an key you can find in wordpress administration in custom fields columns list, but you have to click on "screen options" in top of the screen and then check "Show Field Key". This key looks like 'field_5c7d3d0cbcbd1' and our repeater field name looks like 'orders' .
update_field( 'orders', $value, $user_id );
Will only work OK if you added in administration manually at least one record to repeater field. If you'll try insert with php to empty repeater field, administration page of that post will crash on timeout error.
update_field( 'field_5c7d3d0cbcbd1', $value, $user_id );
Will actually work even when repeater was still empty.
This brings problem when you have multiple wordpress instances (or localhost version) and create tables in administration. Since every single of them will have differrent key.
I wasnt able to find a working way to get key value depending on casual column name. Only solution i could find was to search in database for new created key, and override it on all places (cca 7 of them) to value i have generated on localhost, so same code can work on multiple projects.
Thats only for repeaters, for non repeater acf fields i had no problems using this function :
function acf_updateValue($fieldname, $value, $post_id){
$field_key = 'field_' . uniqid();
update_post_meta($post_id, $fieldname, $value);
update_post_meta($post_id, '_'.$fieldname, $field_key);
update_field($field_key, $value, $post_id);
}
Used for example like -
acf_updateValue('orderFinished', true, $post_id)
- but this did not work with repeater fields.