Title: Saving custom fields during Post Save
Last modified: August 20, 2016

---

# Saving custom fields during Post Save

 *  [treeleaf20](https://wordpress.org/support/users/treeleaf20/)
 * (@treeleaf20)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/saving-custom-fields-during-post-save/)
 * 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.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [treeleaf20](https://wordpress.org/support/users/treeleaf20/)
 * Last activity: [14 years, 3 months ago](https://wordpress.org/support/topic/saving-custom-fields-during-post-save/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
