Using WordPress ‘add_term_relationship’ PHP action

The add_term_relationship WordPress PHP action fires immediately before an object-term relationship is added.

Usage

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

function my_custom_function($object_id, $tt_id, $taxonomy) {
  // your custom code here
  return $object_id;
}

Parameters

  • $object_id (int) – Object ID.
  • $tt_id (int) – Term taxonomy ID.
  • $taxonomy (string) – Taxonomy slug.

More information

See WordPress Developer Resources: add_term_relationship

Examples

Log object-term relationship addition

Log when an object-term relationship is added.

add_action('add_term_relationship', 'log_term_relationship_addition', 10, 3);

function log_term_relationship_addition($object_id, $tt_id, $taxonomy) {
  error_log("Adding a term relationship: Object ID - {$object_id}, Term taxonomy ID - {$tt_id}, Taxonomy - {$taxonomy}");
  return $object_id;
}

Prevent specific term addition

Prevent addition of a specific term to a specific post type.

add_action('add_term_relationship', 'prevent_specific_term_addition', 10, 3);

function prevent_specific_term_addition($object_id, $tt_id, $taxonomy) {
  $forbidden_term = 42;
  $post_type = 'custom_post_type';

  if ($tt_id == $forbidden_term && get_post_type($object_id) == $post_type) {
    wp_remove_object_terms($object_id, $tt_id, $taxonomy);
  }

  return $object_id;
}

Limit maximum terms for a taxonomy

Limit the maximum number of terms for a specific taxonomy.

add_action('add_term_relationship', 'limit_max_terms_for_taxonomy', 10, 3);

function limit_max_terms_for_taxonomy($object_id, $tt_id, $taxonomy) {
  $max_terms = 5;
  $limited_taxonomy = 'custom_taxonomy';

  if ($taxonomy == $limited_taxonomy) {
    $terms = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'ids'));

    if (count($terms) > $max_terms) {
      wp_remove_object_terms($object_id, $terms[0], $taxonomy);
    }
  }

  return $object_id;
}

Send email notification when a term is added

Send an email notification to the admin when a term is added to a specific taxonomy.

add_action('add_term_relationship', 'send_email_on_term_addition', 10, 3);

function send_email_on_term_addition($object_id, $tt_id, $taxonomy) {
  $target_taxonomy = 'custom_taxonomy';

  if ($taxonomy == $target_taxonomy) {
    $admin_email = get_option('admin_email');
    $subject = 'Term Added';
    $message = "A term with term taxonomy ID {$tt_id} was added to the taxonomy {$taxonomy}.";

    wp_mail($admin_email, $subject, $message);
  }

  return $object_id;
}

Set default term for a custom taxonomy

Automatically set a default term for a custom taxonomy when a post is published.

add_action('add_term_relationship', 'set_default_term_for_taxonomy', 10, 3);

function set_default_term_for_taxonomy($object_id, $tt_id, $taxonomy) {
  $default_term_slug = 'default_term';
$custom_taxonomy = 'custom_taxonomy';

if ($taxonomy == $custom_taxonomy) {
$terms = wp_get_object_terms($object_id, $taxonomy);

if (empty($terms)) {
  $default_term = get_term_by('slug', $default_term_slug, $taxonomy);
  wp_set_object_terms($object_id, $default_term->term_id, $taxonomy, true);
}
}

return $object_id;
}