PodsAPI::save_post PHP Method

save_post() public method

Save a post and it's meta
Since: 2.0
public save_post ( array $post_data, array $post_meta = null, boolean $strict = false, boolean $sanitized = false, array $fields = [] ) : mixed | void
$post_data array All post data to be saved (using wp_insert_post / wp_update_post)
$post_meta array (optional) All meta to be saved (set value to null to delete)
$strict boolean (optional) Whether to delete previously saved meta not in $post_meta
$sanitized boolean (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
$fields array (optional) The array of fields and their options, for further processing with
return mixed | void
    public function save_post($post_data, $post_meta = null, $strict = false, $sanitized = false, $fields = array())
    {
        $conflicted = pods_no_conflict_check('post');
        if (!$conflicted) {
            pods_no_conflict_on('post');
        }
        if (!is_array($post_data) || empty($post_data)) {
            $post_data = array('post_title' => '');
        }
        if (!is_array($post_meta)) {
            $post_meta = array();
        }
        if ($sanitized) {
            $post_data = pods_unsanitize($post_data);
            $post_meta = pods_unsanitize($post_meta);
        }
        if (!isset($post_data['ID']) || empty($post_data['ID'])) {
            $post_data['ID'] = wp_insert_post($post_data, true);
        } elseif (2 < count($post_data) || !isset($post_data['post_type'])) {
            $post_data['ID'] = wp_update_post($post_data, true);
        }
        if (is_wp_error($post_data['ID'])) {
            if (!$conflicted) {
                pods_no_conflict_off('post');
            }
            /**
             * @var $post_error WP_Error
             */
            $post_error = $post_data['ID'];
            return pods_error($post_error->get_error_message(), $this);
        }
        $this->save_post_meta($post_data['ID'], $post_meta, $strict, $fields);
        if (!$conflicted) {
            pods_no_conflict_off('post');
        }
        return $post_data['ID'];
    }