RCCWP_Post::SetMetaValues PHP Method

SetMetaValues() public method

Save all custom field values meta values for the post, this function assumes that $_POST['rc_cwp_meta_keys'] contains the names of the fields, while $_POST[{FIELD_NAME}] contains the value of the field named {FIELD_NAME}
public SetMetaValues ( integer $postId ) : void
$postId integer
return void
    function SetMetaValues($postId)
    {
        global $wpdb;
        $customWritePanelId = $_POST['rc-cwp-custom-write-panel-id'];
        //delete file
        if (!empty($_POST['magicfields_remove_files'])) {
            $files = preg_split('/\\|\\|\\|/', $_POST['magicfields_remove_files']);
            foreach ($files as $file) {
                do_action('mf_before_delete_file', $file);
                @unlink(MF_FILES_PATH . $file);
            }
        }
        if (empty($_POST['magicfields'])) {
            return true;
        }
        $customfields = $_POST['magicfields'];
        if ($the_post = wp_is_post_revision($postId)) {
            $postId = $the_post;
        }
        if (!empty($customWritePanelId)) {
            // --- Delete old values
            foreach ($customfields as $name => $field) {
                delete_post_meta($postId, $name);
            }
            $wpdb->delete(MF_TABLE_POST_META, array('post_id' => $postId), array('%d'));
            //Creating the new values
            //Iterating the custom fields
            foreach ($customfields as $name => $groups) {
                $groups_index = 1;
                //Iterating the groups
                foreach ($groups as $group_id => $fields) {
                    $index = 1;
                    //Iterating the  duplicates
                    foreach ($fields as $value) {
                        // Adding field value meta data
                        add_post_meta($postId, $name, $value);
                        $fieldMetaID = $wpdb->insert_id;
                        $wpdb->insert(MF_TABLE_POST_META, array('id' => $fieldMetaID, 'field_name' => $name, 'group_count' => $groups_index, 'field_count' => $index, 'post_id' => $postId, 'order_id' => $groups_index), array('%d', '%s', '%d', '%d', '%d', '%d'));
                        //pre save value
                        do_action('mf_presave', $fieldMetaID, $name, $groups_index, $index, $postId, $value, $customWritePanelId);
                        $index++;
                    }
                    $groups_index++;
                }
            }
            if (MF_GET_CACHE_IS_ON) {
                purge_cache_dir(MF_GET_CACHE_DIR . $postId);
            }
        }
    }

Usage Example

Example #1
0
 function SaveCustomFields($postId)
 {
     if (!wp_verify_nonce($_REQUEST['rc-custom-write-panel-verify-key'], 'rc-custom-write-panel')) {
         return $postId;
     }
     if (!current_user_can('edit_post', $postId)) {
         return $postId;
     }
     RCCWP_Post::SetCustomWritePanel($postId);
     RCCWP_Post::PrepareFieldsValues($postId);
     RCCWP_Post::SetMetaValues($postId);
     return $postId;
 }
All Usage Examples Of RCCWP_Post::SetMetaValues