save_post action only when creating a new post

The save_post action also passes three parameters to your callback, one of which being $update which denotes whether the post being saved is an existing post or not.

/**
 * Save post metadata when a post is saved.
 *
 * @param int $post_id The post ID.
 * @param post $post The post object.
 * @param bool $update Whether this is an existing post being updated or not.
 */
function save_post_callback( $post_id, $post, $update ) {

    if ( $update ) {
        return;
    }

    //business logic...

}

add_action( 'save_post', 'save_post_callback', 10, 3 );

See:

Leave a Comment