Post author is changed to admin after his post is modified by admin

The administrative editors could fix the author manually.

Alternatively, you could add custom post meta-data to designate the original author. Then, hooking into the publish_post or transition_post_status actions (or even save_post for that matter) you could check for the presence of the meta-data when a post is being published, and if it exists, replace the post’s author with the original from the meta-data.

Attempting to knock it out with one hook:

function correct_post_data( $strNewStatus, $strOldStatus, $post ) {
    /* Only pay attention to posts (i.e. ignore links, attachments, etc. ) */
    if( $post->post_type !== 'post' )
        return;

    /* If this is a new post, save the original author into the post's meta-data. */
    if( $strOldStatus === 'new' ) {
        update_post_meta( $post->ID, 'original_author', $post->post_author );
    }

    /* If this post is being published, try to restore the original author */
    if( $strNewStatus === 'publish' ) {
         $originalAuthor = get_post_meta( $post->ID, 'original_author' );

         /* If this post has an original author and it's not who the post says it is, revert the author field. */
         if( !empty( $originalAuthor ) && $originalAuthor != $post->post_author ) {
             $postData = array(
                 'ID'           => $post->ID,
                 'post_author'  => $originalAuthor
             );
             wp_update_post( $postData );    //May wish to check if this returns 0 for error-handling
         }
    }
}
add_action( 'transition_post_status', 'correct_post_data' );

A check for !is_admin() in there somewhere could also be useful to confirm that the user is somewhere on the front-end of the site.

Leave a Comment