Hello, I have a function that adds to a repeater via checkboxes (via ACF Form)
This works well, except if I update the post then it continually adds the same values.
See code below. How can I check when I do create the array that the object does not already exist in the repeater? My repeater is field_5ee8874a207d2 and the post object field within this repeater is field_5ee8875b207d3
function my_save_post_gear_pop( $post_id ) {
if( get_post_type($post_id) !== ‘trip’) {
return;
}
// bail early if editing in admin
if( is_admin() ) {
return;
}
//if( have_rows(‘field_5ee8874a207d2’, $post_id) ) {
// return;
//}
// vars
$post = get_post( $post_id );
// get custom fields (field group exists for content_form)
$person_repeater = get_field(‘choices’, $post_id); $countperson = count($person_repeater);
$post_type = get_post_type($post_id);
// change to post type you want them to be able to delete
if ($post_type != ‘trip’) {
return;
}
// delete_field(‘field_5ee8874a207d2’, $post_id);
for($i = 0; $i < $countperson; $i++) {
$field_key = “field_5ee8874a207d2”;
$value = get_field($field_key, $post_id);
$value[] = array(“field_5ee8875b207d3” => “$person_repeater[$i]”);
update_field( $field_key, $value, $post_id );
update_field(‘populate_gear_toggle’, NULL, $post_id);
}
}
add_action(‘acf/save_post’, ‘my_save_post_gear_pop’);