The deleted_postmeta WordPress PHP action fires immediately after deleting metadata for a post.
Usage
add_action('deleted_postmeta', 'your_custom_function'); function your_custom_function($meta_ids) { // your custom code here }
Parameters
- $meta_ids (string[]): An array of metadata entry IDs to delete.
More information
See WordPress Developer Resources: deleted_postmeta
Examples
Logging Deleted Post Meta
Log deleted post meta IDs to a file.
add_action('deleted_postmeta', 'log_deleted_postmeta'); function log_deleted_postmeta($meta_ids) { $log_file = 'deleted_postmeta.log'; $log_data = implode(', ', $meta_ids); file_put_contents($log_file, $log_data, FILE_APPEND); }
Removing Cache after Post Meta Deletion
Remove cache for a specific post after its metadata is deleted.
add_action('deleted_postmeta', 'remove_cache_on_meta_deletion'); function remove_cache_on_meta_deletion($meta_ids) { foreach ($meta_ids as $meta_id) { $post_id = get_metadata_by_mid('post', $meta_id)->post_id; wp_cache_delete($post_id, 'posts'); } }
Sending Email Notification on Post Meta Deletion
Send email notifications to the admin when post metadata is deleted.
add_action('deleted_postmeta', 'send_email_on_meta_deletion'); function send_email_on_meta_deletion($meta_ids) { $to = get_option('admin_email'); $subject = 'Post Metadata Deleted'; $message = 'The following post metadata IDs have been deleted: ' . implode(', ', $meta_ids); wp_mail($to, $subject, $message); }
Auditing Deleted Post Meta
Audit the deleted post meta by storing the deleted data in a custom table.
add_action('deleted_postmeta', 'audit_deleted_postmeta'); function audit_deleted_postmeta($meta_ids) { global $wpdb; $table_name = $wpdb->prefix . 'deleted_postmeta_audit'; foreach ($meta_ids as $meta_id) { $wpdb->insert($table_name, array('meta_id' => $meta_id)); } }
Adding Custom Action on Post Meta Deletion
Trigger a custom action after post metadata is deleted.
add_action('deleted_postmeta', 'custom_action_on_meta_deletion'); function custom_action_on_meta_deletion($meta_ids) { // your custom code here do_action('my_custom_action', $meta_ids); }