save()

Save custom field values.

Parameters

CFS()->save($field_data, $post_data);
Parameter Type Required Summary
$field_data array yes An associative array of field data
$post_data array yes An associative array of post data. To update an existing post, set $post_data['ID'].

$post_data can include other post attributes, such as:

  • $post_data['post_title']
  • $post_data['post_type']
  • $post_data['post_status']
  • $post_data['post_content']
  • $post_data['post_date']
  • $post_data['post_author']

Return Value

(int) The post ID

Examples

Update an existing post
$field_data = array('first_name' => 'Matt');
$post_data = array('ID' => 678); // the ID is required
CFS()->save($field_data, $post_data);
Create post with custom fields
Create a new post titled "My Post", setting the "first_name" and "last_name" fields
$field_data = array('first_name' => 'Bob', 'last_name' => 'Jones');
$post_data = array('post_title' => 'My Post', 'post_type' => 'person');
CFS()->save($field_data, $post_data);
Save loop field values
Loops are arrays of associative arrays.
$field_data = array(
    'text_field' => 'Value',
    'loop_field' => array(
        array(
            'loop_text' => 'Value',
            'loop_date' => '2013-01-01 10:30:00',
        ),
        array(
            'loop_text' => 'Value',
            'loop_date' => '2013-01-02 12:45:00',
        ),
    ),
);
Copy custom fields from one post to another
Copy custom field values from one post (ID=123) to another (ID=456). Note the use of get()'s options parameter. Setting "format" => "raw" prevents CFS from formatting the raw values.
$options = array('format' => 'raw');
$field_data = CFS()->get(false, 123, $options);
$post_data = array('ID' => 456);
CFS()->save($field_data, $post_data);