php – Assign new post author if there is another user in custom field on post transition

I have a post that is being created on behalf of someone else – so a team member adds a post for the author using the frontend form, but when the post status changes to publish, I want the selected user The Custom Fields in the box be updated as to the post author.

And I don’t want to go into the admin dashboard to update the author, because the team member doesn’t have access and that would lead to a lot of unnecessary admins!

I thought I could use the publish_post hook (or ‘transferring_post’ for me because I use custom post conditions). But then I found:

add_filter("the_author", "change_author");
function change_author($author){
$author "NEW AUTHOR!";
return $author; }

However, how do I then update this to fire only on a post transition in my custom post status ‘transfer’? If it does I’ll need to work out then to update the status of the post as well – but that might be my next question…

(Maybe this is obvious, but I’m still learning!) Thanks!

Leave a Comment