1

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);
    }
    };}

    }

10
  • Could it not have that author from the beginning instead of changing author on publish?
    – Tom J Nowell
    Commented Oct 25, 2022 at 10:45
  • Hey Tom - thanks for the suggestion - do you mean if the author ($preparer) who is writing the post on behalf of the public author ($publicauthor) changes the post author on the WP backend? That would certainly work if I had a normal setup. But the $preparer are also external - so I have multiple user dashboards to try to make it an easier user experience for the $preparer. So they also submit via frontend. Plus I only want the post author to transition when the $preparer is ready. Commented Oct 25, 2022 at 12:40
  • No, if Alice is writing a post on behalf of Bob, then the author would be and always will have been Bob. It would never have Alice as the author, and no switching would occur because none is necessary. I'm not sure why Alice would ever be the author, it sounds like you're trying to automate a solution to a different problem you haven't shared, aka Alice can't see posts she's writing on other peoples behalf in your custom dashboards unless the author is set to Alice. That sounds like an easier problem to solve.
    – Tom J Nowell
    Commented Oct 25, 2022 at 13:41
  • it also sounds like you have no system for writing articles on behalf of other people, other than the manual process to changing the author on publish. The problem is that to solve the original problem you would need to know which posts are written on behalf of people, but you can't avoid that problem because you need to solve it to automate your workaround. AKA How does it know that you want Alices posts to change their author on publish to Bob? And if you know how to determine that, why not just show those in the dashboard anyway?
    – Tom J Nowell
    Commented Oct 25, 2022 at 13:43
  • 1
    Oh I'm sorry. Added to question in an edit Commented Oct 25, 2022 at 17:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.