I have added a custom meta box for advanced information for a specific post category, to my create new post page.

Now I recognize, that if I save the post, it writes 2 entries in the db with 2 post_ids.

add_action( 'add_meta_boxes', 'my-plugin_add_custom_box' );
add_action( 'save_post', 'my-plugin_save_postdata' );

function my-plugin_add_custom_box() {
        __( 'my-plugin', 'my-plugin_textdomain' ),

/* When the post is saved, saves our custom data */
function my-plugin_save_postdata( $post_id ) {
  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST['my-plugin_noncename'], plugin_basename( __FILE__ ) ) )

  // Check permissions
  if ( 'page' == $_POST['post_type'] ) //is this the correct post_type?
    if ( !current_user_can( 'edit_page', $post_id ) )
    if ( !current_user_can( 'edit_post', $post_id ) )

  // OK, we're authenticated: we need to find and save the data

    //$mydata = $_REQUEST['sports'];
    print($post_id); //prints two post ids e.g. 831 & 832
    //print($mydata); //prints two times content of $mydata

Why are two records in the tables wp_posts are created? And when I update the post, a new record is created with post_name post_id-revision (843-revision). What are the advantages for creating a new post type, like sports for such kind of posts? My advanced information like $_REQUEST['sports']; are planned to be stored in a separate db with a ref to wp_posts.

Thanks in advance & BR,


  • i have a same issue please define which action to use for save function "update_post".
    – Ravi Patel
    May 6, 2014 at 6:04

3 Answers 3


One of the two IDs might be a post revision. To prevent this behaviour, I always have this checks in my save_postdata function:

// Autosave, do nothing
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
// AJAX? Not used here
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
// Check user permissions
if ( ! current_user_can( 'edit_post', $post_id ) )
// Return if it's a post revision
if ( false !== wp_is_post_revision( $post_id ) )

As you can see, the last condition I use here checks for the post revision. Try to use this in your function, too.

  • currently I solved my issue, with using 'update_post'. Thx for your answer I will test it :)
    – mybecks
    May 16, 2012 at 9:14

An Example for read how to.

In the your function my-plugin_save_postdata()do you must update the meta data, use the function update_post_meta() for doing this, not more; only the $_POST is true. Also, WP save an new entry in the db, if you have not deactivate the revision. All updates of an post, include autosave, create an new post with an new id. You can kill this doing via constant in the wp-config.php - define( 'WP_POST_REVISIONS', FALSE );

  • One problem is, that it currently prints out the values two times for e.g. soccerscoocer or the id 841842. When I add my db insert for my sports table, I have 2 entries in there with the same values, it messes up my table. Would this be solved, if I disable the revisions in wp?
    – mybecks
    May 10, 2012 at 8:40

Actually, there are some instructions to be followed.

  • Firstly, counter should be set.

global $pr_leasing_counter; // If this is just a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) ) return; // echo '

'; print_r( $post );

if ($post->post_type == 'leasing' && $pr_leasing_counter!=1 && $update == 1 && $post->post_status == 'publish' ) { $pr_leasing_counter = 1 }
  • Secondly, this counter will be further set in the condition.
  • global $pr_leasing_counter; // If this is just a revision, don't send the email. if ( wp_is_post_revision( $post_id ) ) return; if ($post->post_type == 'leasing' && $pr_leasing_counter!=1 && $update == 1 && $post->post_status == 'publish' ) { $pr_leasing_counter = 1 } Aug 12, 2018 at 5:33
  • above is the proper code for same. Aug 12, 2018 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.