The deleted_term_relationships WordPress PHP action fires immediately after an object-term relationship is deleted.
Usage
add_action('deleted_term_relationships', 'your_custom_function', 10, 3); function your_custom_function($object_id, $tt_ids, $taxonomy) { // your custom code here }
Parameters
- $object_id (int) – Object ID.
- $tt_ids (array) – An array of term taxonomy IDs.
- $taxonomy (string) – Taxonomy slug.
More information
See WordPress Developer Resources: deleted_term_relationships
Examples
Logging Deleted Term Relationships
Log the details of deleted term relationships in a custom log file.
add_action('deleted_term_relationships', 'log_deleted_term_relationships', 10, 3); function log_deleted_term_relationships($object_id, $tt_ids, $taxonomy) { // Create a log entry $log_entry = 'Deleted term relationships: Object ID - ' . $object_id . ', Taxonomy - ' . $taxonomy . ', Term Taxonomy IDs - ' . implode(', ', $tt_ids) . PHP_EOL; // Append the log entry to the log file file_put_contents('deleted_term_relationships.log', $log_entry, FILE_APPEND); }
Clear Cache After Deleting Term Relationships
Clear a cache after a term relationship is deleted.
add_action('deleted_term_relationships', 'clear_cache_on_term_relationship_delete', 10, 3); function clear_cache_on_term_relationship_delete($object_id, $tt_ids, $taxonomy) { // Clear the cache your_custom_cache_clearing_function(); }
Notify Admins About Deleted Term Relationships
Send an email to the site administrators when a term relationship is deleted.
add_action('deleted_term_relationships', 'notify_admins_on_term_relationship_delete', 10, 3); function notify_admins_on_term_relationship_delete($object_id, $tt_ids, $taxonomy) { // Prepare email subject and message $subject = 'Term Relationship Deleted'; $message = 'A term relationship has been deleted with the following details:' . PHP_EOL; $message .= 'Object ID: ' . $object_id . PHP_EOL; $message .= 'Taxonomy: ' . $taxonomy . PHP_EOL; $message .= 'Term Taxonomy IDs: ' . implode(', ', $tt_ids) . PHP_EOL; // Send the email to the site administrators wp_mail(get_option('admin_email'), $subject, $message); }
Update Custom Counter on Term Relationship Deletion
Update a custom counter when a term relationship is deleted.
add_action('deleted_term_relationships', 'update_custom_counter', 10, 3); function update_custom_counter($object_id, $tt_ids, $taxonomy) { // Update the custom counter $counter = get_option('custom_term_relationship_counter', 0); $counter--; update_option('custom_term_relationship_counter', $counter); }
Track Deleted Term Relationships for Custom Reporting
Store the deleted term relationship details in a custom table for reporting purposes.
add_action('deleted_term_relationships', 'track_deleted_term_relationships', 10, 3); function track_deleted_term_relationships($object_id, $tt_ids, $taxonomy) { global $wpdb; // Insert the deleted term relationship details into the custom table $table_name = $wpdb->prefix . 'deleted_term_relationships'; $data = array( 'object_id' => $object_id, 'taxonomy' => $taxonomy, 'term_taxonomy_ids' => implode(', ', $tt_ids), 'deleted_at' => current_time('mysql'), ); $wpdb->insert($table_name, $data); }