The deleted_{$meta_type}_meta WordPress PHP action fires immediately after deleting metadata of a specific type. The dynamic portion of the hook name, $meta_type, refers to the meta object type (post, comment, term, user, or any other type with an associated meta table).
Usage
add_action('deleted_{$meta_type}_meta', 'your_custom_function', 10, 4);
function your_custom_function($meta_ids, $object_id, $meta_key, $_meta_value) {
// your custom code here
}
Parameters
- $meta_ids: string[] – An array of metadata entry IDs to delete.
- $object_id: int – ID of the object metadata is for.
- $meta_key: string – Metadata key.
- $_meta_value: mixed – Metadata value.
More information
See WordPress Developer Resources: deleted_{$meta_type}_meta
Examples
Log deleted post metadata
Log the deleted post metadata to a custom log file.
add_action('deleted_post_meta', 'log_deleted_post_meta', 10, 4);
function log_deleted_post_meta($meta_ids, $object_id, $meta_key, $_meta_value) {
// Log the deleted post meta information
error_log("Deleted post meta (ID: {$object_id}, Key: {$meta_key}, Value: {$_meta_value})");
}
Update comment count when deleting a specific meta key
Update the comment count for a post when a specific comment meta key is deleted.
add_action('deleted_comment_meta', 'update_comment_count_on_meta_delete', 10, 4);
function update_comment_count_on_meta_delete($meta_ids, $object_id, $meta_key, $_meta_value) {
if ($meta_key === 'my_custom_key') {
$comment = get_comment($object_id);
$post_id = $comment->comment_post_ID;
wp_update_comment_count($post_id);
}
}
Clear user cache when user metadata is deleted
Clear user cache when any user metadata is deleted.
add_action('deleted_user_meta', 'clear_user_cache_on_meta_delete', 10, 4);
function clear_user_cache_on_meta_delete($meta_ids, $object_id, $meta_key, $_meta_value) {
wp_cache_delete($object_id, 'users');
}
Delete term thumbnail when term metadata is deleted
Remove term thumbnail when term metadata is deleted.
add_action('deleted_term_meta', 'remove_term_thumbnail_on_meta_delete', 10, 4);
function remove_term_thumbnail_on_meta_delete($meta_ids, $object_id, $meta_key, $_meta_value) {
if ($meta_key === 'thumbnail_id') {
wp_delete_attachment($_meta_value, true);
}
}
Send notification when custom metadata is deleted
Send a notification email to the admin when a specific custom metadata is deleted.
add_action('deleted_post_meta', 'send_notification_on_custom_meta_delete', 10, 4);
function send_notification_on_custom_meta_delete($meta_ids, $object_id, $meta_key, $_meta_value) {
if ($meta_key === 'my_custom_metadata') {
$admin_email = get_option('admin_email');
wp_mail($admin_email, 'Custom Metadata Deleted', "Custom metadata (ID: {$object_id}, Key: {$meta_key}, Value: {$_meta_value}) has beendeleted.");
}
}
Log deleted post metadata
Log deleted post metadata to a custom log file.
add_action('deleted_post_meta', 'log_deleted_postmeta', 10, 1);
function log_deleted_postmeta($meta_id) {
// Log the deleted post metadata ID
error_log("Deleted post metadata ID: " . $meta_id);
}
Clear cache after deleting comment metadata
Clear a custom cache when comment metadata is deleted.
add_action('deleted_comment_meta', 'clear_commentmeta_cache', 10, 1);
function clear_commentmeta_cache($meta_id) {
// Clear the custom cache
your_cache_clear_function();
}
Notify admin when term metadata is deleted
Send an email notification to the admin when term metadata is deleted.
add_action('deleted_term_meta', 'notify_admin_on_termmeta_deletion', 10, 1);
function notify_admin_on_termmeta_deletion($meta_id) {
// Send an email notification to the admin
wp_mail(get_option('admin_email'), 'Term Metadata Deleted', 'Term metadata with ID ' . $meta_id . ' has been deleted.');
}
Update user activity when user metadata is deleted
Update user activity records when user metadata is deleted.
add_action('deleted_user_meta', 'update_user_activity_on_usermeta_deletion', 10, 1);
function update_user_activity_on_usermeta_deletion($meta_id) {
// Update the user activity records
your_update_user_activity_function($meta_id);
}
Perform custom action after deleting post metadata
Perform a custom action after post metadata is deleted.
add_action('deleted_post_meta', 'perform_custom_action_on_postmeta_deletion', 10, 1);
function perform_custom_action_on_postmeta_deletion($meta_id) {
// Perform your custom action here
}