Using WordPress ‘delete_term_relationships’ PHP action

The delete_term_relationships WordPress PHP action fires immediately before an object-term relationship is deleted.

Usage

add_action('delete_term_relationships', 'my_custom_function', 10, 3);

function my_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: delete_term_relationships

Examples

Log term relationship deletion

Log the term relationship deletion details in a custom log file.

function log_term_relationship_deletion($object_id, $tt_ids, $taxonomy) {
    $log_message = "Term relationship deleted for object ID: {$object_id}, taxonomy: {$taxonomy}, term taxonomy IDs: " . implode(', ', $tt_ids);
    error_log($log_message, 3, '/path/to/your/custom_log.log');
}
add_action('delete_term_relationships', 'log_term_relationship_deletion', 10, 3);

Send email notification on term relationship deletion

Send an email notification to the administrator when a term relationship is deleted.

function email_on_term_relationship_deletion($object_id, $tt_ids, $taxonomy) {
    $subject = "Term relationship deleted";
    $message = "A term relationship has been deleted for object ID: {$object_id}, taxonomy: {$taxonomy}, term taxonomy IDs: " . implode(', ', $tt_ids);
    wp_mail('[email protected]', $subject, $message);
}
add_action('delete_term_relationships', 'email_on_term_relationship_deletion', 10, 3);

Remove custom data on term relationship deletion

Remove custom data associated with the term relationship when it’s deleted.

function remove_custom_data_on_term_relationship_deletion($object_id, $tt_ids, $taxonomy) {
    foreach ($tt_ids as $tt_id) {
        delete_metadata('term', $tt_id, 'custom_data_key');
    }
}
add_action('delete_term_relationships', 'remove_custom_data_on_term_relationship_deletion', 10, 3);

Update post count on term relationship deletion

Update a custom post count for a specific taxonomy when a term relationship is deleted.

function update_post_count_on_term_relationship_deletion($object_id, $tt_ids, $taxonomy) {
    if ('custom_taxonomy' === $taxonomy) {
        $post_count = get_option('custom_taxonomy_post_count', 0);
        $post_count--;
        update_option('custom_taxonomy_post_count', max(0, $post_count));
    }
}
add_action('delete_term_relationships', 'update_post_count_on_term_relationship_deletion', 10, 3);

Perform custom action for a specific term relationship deletion

Perform a custom action only when a specific term relationship is deleted.

function custom_action_on_specific_term_relationship_deletion($object_id, $tt_ids, $taxonomy) {
    if (in_array(123, $tt_ids) && 'custom_taxonomy' === $taxonomy) {
        // Perform your custom action here
    }
}
add_action('delete_term_relationships', 'custom_action_on_specific_term_relationship_deletion', 10, 3);