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

I am writing a plugin which will enable to write posts from front-end. Those posts have to be checked by admin before published. Now if admin edits the post or publishes the post, the post author is changed from the original author to admin. How can I prevent that?

Related posts

Leave a Reply

3 comments

  1. 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.

  2. I experienced the same problem a couple of weeks ago. My problem was that I was using a custom post type and I didn’t add support for author.
    It was always published by the correct author but when admin changed post status or updated the post the admin becomes the post author.

    Try adding support for author and see if that helps!

  3. That seems like a very strange issue. The status of a post should not affect the user setting.

    Have you considered using Gravity Forms? It is a paid plugin but it does a nice job with creating the forms and it is relatively simple to create a front end form that creates a post automatically (in either published or draft or review state).