• Then I have the following function to save my data:

    function myplugin_save_postdata ( $post_id ){
    if (defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    
    if (!wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename ( __FILE__ ) ) )
        return;
    
    if ( 'page' == $_POST['post_type'] ){
        if (!current_user_can( 'edit_page', $post_id ) )
            return;
    }else{
        if (!current_user_can( 'edit_post', $post_id ) )
            return;
    }
    
    $address1 = mysql_real_escape_string($_POST['address1']);
    $address2 = mysql_real_escape_string($_POST['address2']);
    $city = mysql_real_escape_string($_POST['city']);
    $state = mysql_real_escape_string($_POST['state']);
    $zip = mysql_real_escape_string($_POST['zip']);
    
    $sql = "Insert into custom_vendor_info (post_id, address1, address2, city, state, zip) values('$post_id', '$address1', '$address2', '$city', '$state', '$zip')";
    $_SESSION['my_admin_notices'] .= '<div class="updated"><p>'.$sql.'</p></div>';
    $reuslt = mysql_query($sql);
    return true;
    }

    However, I’m getting two entries into my custom table. I need to get the post id that WordPress saves originally so I can have a link between my custom table and the wp_posts table. How can I do this? Thanks in advance!

The topic ‘Saving custom fields during Post Save’ is closed to new replies.