0

I have a case where there will be multiple users writing, editing, and publishing posts. All these posts should always have the same user as the author, a catch-all user if you will.

Is it possible to programmatically set the author for every new post to this user, no matter which user is logged in? I think I will be able to figure out how to remove the ability to change author on posts, but I am kind of stuck on this one question.

If someone could point me in the right direction I would be very thankful!

1

This is untested, but you could use the wp_insert_post_data filter to change the post author to a specific value whenever a post is inserted or updated:

add_filter(
    'wp_insert_post_data',
    function( $data ) {
        if ( 'post' === $data['post_type'] ) {
            $data['post_author'] = 2; // Replace with desired author's user ID.
        }

        return $data;
    }
);

Just be aware that if posts are being created by users who do not have the edit_others_posts capability, such as Authors and Contributors, then they will experience unusual behaviour, because they will not have permission to do anything with the post once it has been saved. So they could see an error when the post page reloads after pressing Publish, for example.

| improve this answer | |
  • 1
    Worked a treat, thanks! And also, thanks for taking the time to explain the possible problems. Have a nice day! – TASan Apr 20 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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