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!
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!
Share Improve this question asked Apr 20, 2020 at 5:41 TASanTASan 421 silver badge9 bronze badges1 Answer
Reset to default 1This 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.