I think you want to use the added_post_meta
hook instead of updated_post_meta
because you’re not updating the meta here, only adding it. At least in the case of the _thumbnail_id
, where we must delete it before adding it again (no update) through the admin UI.
Investigating this further we see that this part of the update_metadata()
function:
if ( empty( $meta_ids ) ) {
return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
}
is causing you the problem, because it calls add_metadata()
and returns it, before the update_{$meta_type}_meta
and updated_{$meta_type}_meta
hooks are ever fired.
You therefore need to hook into the add_metadata()
function, instead of the update_metadata()
function, through e.g. the add_{$meta_type}_meta
(before) or added_{$meta_type}_meta
(after) hooks.
If we check out the wp_ajax_set_post_thumbnail()
function, that’s ajax-requested from the admin UI when adding/removing the featured image, we see that it uses the functions
set_post_thumbnail()
and delete_post_thumbnail()
.
The latter one is a wrapper for delete_metadata()
, that fires up the delete_{$meta_type}_meta
(before) and deleted_{$meta_type}_meta
(after) hooks.