Callback hooked to post_updated firing on new posts as well
Context: I'm using a plugin that allows me to add author boxes throughout my site (Simple Author Box), while providing an easy way for any user to update their social media links and profile pictures. I figured I could also leverage that plugin to dynamically show the team members in the site's about section.
However, I didn't want to have author archives for the team members that won't be publishing any posts in the future (designer, myself etc.), so I used the solution from this question as a starting point to disable specific users' author archives and from there I created a few additional functions in order to automate that functionality (author archives are now automatically being disabled/enabled according to the number of posts each user has published).
One of these functions is hooked to post_updated
, which according to the docs: Fires once an existing post has been updated. [emphasis added]
Here's the function's code (please forgive any lack of good practices, I'm new to PHP and not an experienced programmer):
/*
* This function does the checks and the actual value update, if needed.
* It's called from inside the callback.
*/
function maybe_update_author_archive_status($user_id, $published_posts, $author_archive_disabled) {
if ($published_posts == 0 $author_archive_disabled != 'on') {
update_user_meta($user_id, '_author_archive_disabled', 'on');
} elseif ($published_posts != 0 $author_archive_disabled != 'off') {
update_user_meta($user_id, '_author_archive_disabled', 'off');
}
}
/*
* The callback itself.
*/
function maybe_update_author_archive_status_on_post_update($post_id, $post_after, $post_before) {
if($post_before-post_status != 'publish' $post_after-post_status != 'publish') {
return;
}
$old_author = $post_before-post_author;
$new_author = $post_after-post_author;
$authors = array($old_author);
/* If the post author has changed, I might need to update both author archive status */
if($new_author != $old_author) {
$authors[] = $new_author;
}
foreach($authors as $author) {
$user_id = intval($author, 10);
$author_archive_disabled = get_user_meta($user_id, '_author_archive_disabled', true);
$published_posts = count_user_posts($user_id);
maybe_update_author_archive_status($user_id, $published_posts, $author_archive_disabled);
}
}
add_action('post_updated', 'maybe_update_author_archive_status_on_post_update', 10, 3);
However, to my surprise (and delight actually), it's also firing when I create and publish a new post. Can anyone explain me why? Under which circumstances this function wouldn't be fired? Even though this is the behavior I desired and everything is working as I wanted it to, this is not what I expected it would happen after reading the documentation.