$current_user = wp_get_current_user();
// Create WP post, my custom post type is called 'profiles'
$my_post = array(
'post_title' => $dname,
'post_content' => '',
'post_status' => 'draft',
'post_type' => 'profiles',
'post_author' => $current_user->ID
);
// Create array of all the custom fields in my Magic Fields custom post type
$post_metas = array('profile_username', 'profile_display_in', 'first_name', 'middle_name', 'last_name', 'credentials', 'education', 'certifications', 'profile_specialty', 'phone', 'fax', 'email', 'custom_title', 'position_position', 'position_group', 'position_link', 'position_hidden', 'research_year', 'research_description', 'research_hidden', 'publication_year', 'publication', 'publication_link', 'clinic_specialty', 'clinic_language', 'awards_and_honor_year', 'awards_and_honor_award');
// Insert the post into the database
$the_post_id = wp_insert_post( $my_post );
// Function I used to get the meta_id of the custom field
function get_mid_by_key( $post_id, $meta_key ) {
global $wpdb;
$mid = $wpdb->get_var( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key) );
if( $mid != '' )
return (int)$mid;
return false;
}
// For each custom field in my Magic Fields custom post type
foreach($post_metas as $post_meta){
add_post_meta($the_post_id, $post_meta, ''); // This creates a blank custom_field entry to start
$meta_id = get_mid_by_key( $the_post_id, $post_meta ); // Get the ID of that blank field we just inserted
// Now update mf_post_meta with a field_count and group_count of 1 to let magic fields know that the custom field has values, if you have more than one of the same custom field you'll have to adjust this
$wpdb->insert(
'wp_mf_post_meta',
array(
'meta_id' => $meta_id,
'field_name' => $post_meta,
'field_count' => 1,
'group_count' => 1,
'post_id' => $the_post_id
)
);
}
finally update the custom fields with the real value, I get my values from a GET request
update_post_meta($the_post_id, 'profile_username', $_GET['uname']);
update_post_meta($the_post_id, 'first_name', $_GET['fname']);
update_post_meta($the_post_id, 'last_name', $_GET['lname']);
update_post_meta($the_post_id, 'phone', $_GET['phone']);
update_post_meta($the_post_id, 'email', $_GET['email']);
update_post_meta($the_post_id, 'position_position', $position);
update_post_meta($the_post_id, 'position_group', $group);
[/code]