I have a post that is being made on behalf of someone else - so a team member adds the post for the author using a frontend form, but when the post status changes to publish, I want the selected user in the custom field box to be updated as the post author.
And I don't want to go into the admin dashboard to update the author, as the team member does not have access and it would cause a lot of unneeded admin!
I thought I could use the publish_post hook (or 'transferring_post' for me as I use custom post statuses). 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 only fire on a post transition to my custom post status 'transferring'?
Edit (for anyone looking to do something similar - this works)
add_action( 'transition_post_status','transfer_to_review_transition_post_status',10, 3);
function transfer_to_review_transition_post_status($new_status, $old_status, $post){
if (($old_status == 'transferring') && ($new_status == 'review') && ($post->post_type == 'research')){
$post_id = $post->ID;
$oldauthor = get_post_field( 'post_author', $post_id );
$newauthor = rwmb_meta( 'academicinterview_transfer_author_to_academic', '', $post_id );
if (!empty($newauthor) && ($newauthor != $oldauthor) ) {
$postarr = array(
'ID' => $post_id,
'post_author' => $newauthor,
);
wp_update_post($postarr);
}
};}
}