Set a User as Author of all ‘New Posts’ posted

function wp84782_replace_author( $post_ID )  
{
  $my_post = array();
  $my_post['ID'] = $post_ID;
  $my_post['post_author'] = 1 ; //This is the ID number of whatever author you want to assign

// Update the post into the database
  wp_update_post( $my_post );
}
add_action( 'publish_post', 'wp84782_replace_author' );

Update:
This hook runs as the post is being published, not after, so the command is trying to overwrite what the system is doing at the same time. So here’s a modified version that cancels all of this is the post was previously published. If you’re wanting it to never allow user’s to update the author, perhaps you could hide that meta field. I’m not aware of a hook that runs immediately after publish of a post, but if there were one, hooking into it would allow solve this problem.

function wp84782_replace_author( $post_ID )  
{
    if(get_post_status( $post_ID ) == 'publish'){
        return;
    }
    else {
        $my_post = array();
        $my_post['ID'] = $post_ID;
        $my_post['post_author'] = 1 ; //This is the ID number of whatever author you want to assign

        // Update the post into the database
        wp_update_post( $my_post );
    }
}
add_action( 'publish_post', 'wp84782_replace_author' );

Disclaimer: All this code is not tested, so there may be minor edits needed.

Leave a Comment