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.