WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_insert_post (3 posts)

  1. chacha2000
    Member
    Posted 1 year ago #

    Hi, on this page http://codex.wordpress.org/Plugin_API/Action_Reference/wp_insert_post it says it's only called once "wp_insert_post is called with the same parameters as Save_post, but is only called for new posts, after save_post has run. "
    But it's actually also called each time i update a post...
    Does anyone know if it's a bug? Is there a hook to add an action when a post is inserted for the first time only (for real)? (when the post is inserted into the database and given its ID)
    Thanks !

  2. The error is on the unfinished codex page you linked to.

    the wp_insert_post action hook is run anytime a post is updated or created. There is not a specific hook that runs when a post is first inserted into the database. There are specific hooks that run only on update which you can check to see if they have been run in your function.

    add_action( 'wp_insert_post', '_only_on_new' );
    function _only_on_new( $post ) {
         $action = did_action( 'pre_post_update' );
         if ( 0 === $action ) {
            //New post do something here...
        }
    }

    Also to note is that the only time this happens is when you click the new button. That is when the post is first saved and an ID generated.

  3. chacha2000
    Member
    Posted 1 year ago #

    Thanks for clearing that up ! Problem solved !

Topic Closed

This topic has been closed to new replies.

About this Topic