10

Given a user id, $user_id, and post id, $post_id, how can I programatically update a wordpress post author?

Note: these posts are already created and the author cannot be set upon creation. Another process is creating the post and defaulting to the admin as the author. I don't have access to creating the post but have access after it is created.

2 Answers 2

27

It shouldn't be any problem. Try this:

$arg = array(
    'ID' => $post_id,
    'post_author' => $user_id,
);
wp_update_post( $arg );
2
  • this ends up with the post having the user as its author, but if you try to query a users posts it returns nothing - it seems that the user doesn't know about the post when it has been authored that way. It also means that when you delete the user the posts don't get deleted, as they are queried from the deleted user Commented Jan 20, 2021 at 13:03
  • @AbrahamBrookes what you mentioned didn't happen in my case. couldn't confirm :)
    – mdehghani
    Commented Jan 30, 2021 at 20:16
-2

you can also just add another admin account. delete the one that's authoring the posts and WP will ask where to assign posts. Choose preferred author, done.

1
  • Note that the requirement is programmatically.
    – fuxia
    Commented Dec 7, 2018 at 1:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.