wordpress

How can I programmatically set my user as the author when making post revisions?


I’m in the process of updating a bunch of WordPress posts programmatically. I’m using wp_update_post() for this with the ID and post_content arguments. This works fine, but I’d like to log that my user was the one that made the changes so this is shown under revisions.

I assume that when adding post_author to the function, I will be overwriting the original author. If so, how do I add my user to the revision as currently the database shows all changes as done by user 0?


Solution

  • Before update, first get the original author id:

    $author= get_post_field('post_author', $post_id);
    

    then switch the current user to author

       wp_set_current_user($author);
    

    Next update the post.