Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );

my-plugin
function my-plugin_add_custom_box() {
    add_meta_box( 
        'my-plugin_sectionid',
        __( 'my-plugin', 'my-plugin_textdomain' ),
        'my-plugin_inner_custom_box',
        'post' 
    );
}

/* 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 ) 
      return;

  // 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__ ) ) )
      return;


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

  // 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,

mybecks

share|improve this question
add comment

3 Answers

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 ) 
        return;
// AJAX? Not used here
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
        return;
// Check user permissions
if ( ! current_user_can( 'edit_post', $post_id ) )
        return;
// Return if it's a post revision
if ( false !== wp_is_post_revision( $post_id ) )
        return;

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

share|improve this answer
    
currently I solved my issue, with using 'update_post'. Thx for your answer I will test it :) –  mybecks May 16 '12 at 9:14
add comment

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 );

share|improve this answer
    
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 '12 at 8:40
add comment

Add a function or class method... Depending on your setup.

function my_save_post($post_id){
    global $flag;
    if($flag == 0){
        // do stuff ... 
    }
    $flag = 1;
}

Then create a separate function to do what you need when the save_post hook is fired only once.

function my_save_post_execution($post_id){
    // This is where you would update an
    // option or something...
    // Place the code you want to execute 
    // on save_post in this function
}

So the result would look something like this:

function my_save_post($post_id){
    global $flag;
    if($flag == 0){
        // Execute function on save_post only one time
        my_save_post_execution($post_id);   
    }
    $flag = 1;
}

Then just add the action hook:

add_action('save_post', 'my_save_post');

or if you're in class context:

add_action('save_post', array($this, 'my_save_post'));
share|improve this answer
    
Hi Michael. Can you add a description to this post so as to give context to your answer? –  Brian Fegter Aug 27 '12 at 5:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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