function update_value($post_id, $field, $value) { $sub_fields = array(); foreach ($field['layouts'] as $layout) { foreach ($layout['sub_fields'] as $sub_field) { $sub_fields[$sub_field['key']] = $sub_field; } } $total = array(); if ($value) { // remove dummy field unset($value['acfcloneindex']); $i = -1; // loop through rows foreach ($value as $row) { $i++; // increase total $total[] = $row['acf_fc_layout']; unset($row['acf_fc_layout']); // loop through sub fields foreach ($row as $field_key => $v) { $sub_field = $sub_fields[$field_key]; // update full name $sub_field['name'] = $field['name'] . '_' . $i . '_' . $sub_field['name']; // save sub field value $this->parent->update_value($post_id, $sub_field, $v); } } } /* * Remove Old Data * * @credit: http://support.advancedcustomfields.com/discussion/1994/deleting-single-repeater-fields-does-not-remove-entry-from-database */ $old_total = parent::get_value($post_id, $field); $old_total = count($old_total); $new_total = count($total); if ($old_total > $new_total) { foreach ($sub_fields as $sub_field) { for ($j = $new_total; $j < $old_total; $j++) { parent::delete_value($post_id, $field['name'] . '_' . $j . '_' . $sub_field['name']); } } } parent::update_value($post_id, $field, $total); }
function update_value($post_id, $field, $value) { $total = 0; if ($value) { // remove dummy field unset($value['acfcloneindex']); $i = -1; // loop through rows foreach ($value as $row) { $i++; // increase total $total++; // loop through sub fields foreach ($field['sub_fields'] as $sub_field) { // get sub field data $v = isset($row[$sub_field['key']]) ? $row[$sub_field['key']] : ''; // update full name $sub_field['name'] = $field['name'] . '_' . $i . '_' . $sub_field['name']; // save sub field value $this->parent->update_value($post_id, $sub_field, $v); } } } /* * Remove Old Data * * @credit: http://support.advancedcustomfields.com/discussion/1994/deleting-single-repeater-fields-does-not-remove-entry-from-database */ $old_total = (int) parent::get_value($post_id, $field); if ($old_total > $total) { foreach ($field['sub_fields'] as $sub_field) { for ($j = $total; $j < $old_total; $j++) { parent::delete_value($post_id, $field['name'] . '_' . $j . '_' . $sub_field['name']); } } } // update repeater count parent::update_value($post_id, $field, $total); }