function psp_update_sub_field($selector, $value, $post_id = false) { // filter post_id // $post_id = acf_get_valid_post_id( $post_id ); // vars $field = false; $name = ''; // within a have_rows loop if (is_string($selector)) { // loop over global data if (!empty($GLOBALS['acf_field'])) { foreach ($GLOBALS['acf_field'] as $row) { // add to name $name .= "{$row['name']}_{$row['i']}_"; // override $post_id $post_id = $row['post_id']; } } // get sub field $field = get_sub_field_object($selector); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $selector, 'key' => '', 'type' => '')); } // append name $name .= $field['name']; // update name $field['name'] = $name; } elseif (is_array($selector)) { // validate if (count($selector) < 3) { return false; } // vars $parent_name = acf_extract_var($selector, 0); // load parent $field = get_field_object($parent_name, $post_id, false, false); // add to name $name .= "{$field['name']}"; // sub fields foreach ($selector as $s) { if (is_numeric($s)) { $row_i = intval($s) - 1; // add to name $name .= "_{$row_i}"; } else { // update parent $field = acf_get_sub_field($s, $field); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $s, 'key' => '', 'type' => '')); } // add to name $name .= "_{$field['name']}"; } // if } // foreach // update name $field['name'] = $name; } // save return acf_update_value($value, $post_id, $field); }
function acf_maybe_get_sub_field($selectors, $post_id = false, $strict = true) { // bail ealry if not enough selectors if (!is_array($selectors) || count($selectors) < 3) { return false; } // vars $selector = acf_extract_var($selectors, 0); $selectors = array_values($selectors); // reset keys // attempt get field $field = acf_maybe_get_field($selector, $post_id, $strict); // bail early if no field if (!$field) { return false; } // loop for ($j = 0; $j < count($selectors); $j += 2) { // vars $sub_i = $selectors[$j]; $sub_s = $selectors[$j + 1]; $field_name = $field['name']; // find sub field $field = acf_get_sub_field($sub_s, $field); // bail early if no sub field if (!$field) { return false; } // add to name $field['name'] = $field_name . '_' . ($sub_i - 1) . '_' . $field['name']; } // return return $field; }
function update_sub_field($selector, $value, $post_id = false) { // filter post_id $post_id = acf_get_valid_post_id($post_id); // vars $field = false; // within a have_rows loop if (is_string($selector)) { // get current row $row = acf_get_row(); // override $post_id $post_id = $row['post_id']; // get sub field $field = get_sub_field_object($selector, false, false); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $selector, 'key' => '', 'type' => '')); } // update name $field['name'] = "{$row['name']}_{$row['i']}_{$field['name']}"; } elseif (is_array($selector)) { // validate if (count($selector) < 3) { return false; } // vars $parent_name = acf_extract_var($selector, 0); // load parent $field = acf_maybe_get_field($parent_name, $post_id); // add to name $name = "{$field['name']}"; // sub fields foreach ($selector as $s) { if (is_numeric($s)) { $row_i = intval($s) - 1; // add to name $name .= "_{$row_i}"; } else { // update parent $field = acf_get_sub_field($s, $field); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $s, 'key' => '', 'type' => '')); } // add to name $name .= "_{$field['name']}"; } // if } // foreach // update name $field['name'] = $name; } // delete if ($value === null) { return acf_delete_value($post_id, $field); } // update return acf_update_value($value, $post_id, $field); }
function get_row_sub_field($selector) { // vars $row = acf_get_loop('active'); // bail early if no row if (!$row) { return false; } // attempt to find sub field $sub_field = acf_get_sub_field($selector, $row['field']); // bail early if no field if (!$sub_field) { return false; } // update field's name based on row data $sub_field['name'] = "{$row['name']}_{$row['i']}_{$sub_field['name']}"; // return return $sub_field; }