function acf_copy_postmeta($from_post_id, $to_post_id) { // get all postmeta $meta = get_post_meta($from_post_id); // bail early if no meta if (!$meta) { return; } // loop foreach ($meta as $name => $value) { // attempt to find key value $key = acf_maybe_get($meta, '_' . $name); // bail ealry if no key if (!$key) { continue; } // update vars $value = $value[0]; $key = $key[0]; // bail early if $key is a not a field_key if (!acf_is_field_key($key)) { continue; } // get_post_meta will return array before running maybe_unserialize $value = maybe_unserialize($value); // add in slashes // - update_post_meta will unslash the value, so we must first slash it to avoid losing backslashes // - https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping if (is_string($value)) { $value = wp_slash($value); } // update value acf_update_metadata($to_post_id, $name, $value); acf_update_metadata($to_post_id, $name, $key, true); } }
function delete_row($selector, $row = 1, $post_id = false) { // filter post_id $post_id = acf_get_valid_post_id($post_id); // get field $field = acf_maybe_get_field($selector, $post_id); // bail early if no field if (!$field) { return false; } // get value $rows = acf_get_value($post_id, $field); // bail early if no value if (empty($rows)) { return false; } // deincrement if ($row == count($rows)) { acf_update_metadata($post_id, $field['name'], $row - 1); } // update sub field values foreach ($rows[0] as $k => $v) { update_sub_field(array($field['key'], $row, $k), null, $post_id); } // return return true; }
function acf_update_value($value = null, $post_id = 0, $field) { // vars $return = false; // strip slashes if (acf_get_setting('stripslashes')) { $value = stripslashes_deep($value); } // filter for 3rd party customization $value = apply_filters("acf/update_value", $value, $post_id, $field); $value = apply_filters("acf/update_value/type={$field['type']}", $value, $post_id, $field); $value = apply_filters("acf/update_value/name={$field['name']}", $value, $post_id, $field); $value = apply_filters("acf/update_value/key={$field['key']}", $value, $post_id, $field); // update value $return = acf_update_metadata($post_id, $field['name'], $value); // update reference acf_update_metadata($post_id, '_' . $field['name'], $field['key']); // clear cache wp_cache_delete("load_value/post_id={$post_id}/name={$field['name']}", 'acf'); // return return $return; }
function add_row($selector, $value, $post_id = false) { // filter post_id $post_id = acf_get_valid_post_id($post_id); // get field $field = acf_maybe_get_field($selector, $post_id); // bail early if no field if (!$field) { return false; } // get row count $i = (int) acf_get_metadata($post_id, $field['name']); // if no rows, save this field via update_field() so that the reference field is created if (!$i) { // acf_update_value will return boolean, simply convert this to int for 1 | 0 (the number of rows!) return (int) acf_update_value(array($value), $post_id, $field); } // increase $i $i++; // update meta $result = acf_update_metadata($post_id, $field['name'], $i); // update sub fields if ($value) { foreach ($value as $k => $v) { update_sub_field(array($field['key'], $i, $k), $v, $post_id); } } // return return $i; }