* and add the alias to it.
$term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;
'term_group' => $term_group,
* Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy,
* unless a unique slug has been explicitly provided.
$name_matches = get_terms(
'parent' => $args['parent'],
'update_term_meta_cache' => false,
* The `name` match in `get_terms()` doesn't differentiate accented characters,
* so we do a stricter comparison here.
foreach ( $name_matches as $_match ) {
if ( strtolower( $name ) === strtolower( $_match->name ) ) {
$slug_match = get_term_by( 'slug', $slug, $taxonomy );
if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) {
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
'update_term_meta_cache' => false,
$sibling_names = wp_list_pluck( $siblings, 'name' );
$sibling_slugs = wp_list_pluck( $siblings, 'slug' );
if ( ( ! $slug_provided || $name_match->slug === $slug ) && in_array( $name, $sibling_names, true ) ) {
$existing_term = $name_match;
} elseif ( $slug_match && in_array( $slug, $sibling_slugs, true ) ) {
$existing_term = $slug_match;
return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $existing_term->term_id );
return new WP_Error( 'term_exists', __( 'A term with the name provided already exists in this taxonomy.' ), $name_match->term_id );
$slug = wp_unique_term_slug( $slug, (object) $args );
$data = compact( 'name', 'slug', 'term_group' );
* Filters term data before it is inserted into the database.
* @param array $data Term data to be inserted.
* @param string $taxonomy Taxonomy slug.
* @param array $args Arguments passed to wp_insert_term().
$data = apply_filters( 'wp_insert_term_data', $data, $taxonomy, $args );
if ( false === $wpdb->insert( $wpdb->terms, $data ) ) {
return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database.' ), $wpdb->last_error );
$term_id = (int) $wpdb->insert_id;
// Seems unreachable. However, is used in the case that a term name is provided, which sanitizes to an empty string.
$slug = sanitize_title( $slug, $term_id );
/** This action is documented in wp-includes/taxonomy.php */
do_action( 'edit_terms', $term_id, $taxonomy );
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
/** This action is documented in wp-includes/taxonomy.php */
do_action( 'edited_terms', $term_id, $taxonomy );
$tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) );
if ( ! empty( $tt_id ) ) {
'term_taxonomy_id' => $tt_id,
if ( false === $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ) + array( 'count' => 0 ) ) ) {
return new WP_Error( 'db_insert_error', __( 'Could not insert term taxonomy into the database.' ), $wpdb->last_error );
$tt_id = (int) $wpdb->insert_id;
* Confidence check: if we just created a term with the same parent + taxonomy + slug but a higher term_id than
* an existing term, then we have unwittingly created a duplicate term. Delete the dupe, and use the term_id
* and term_taxonomy_id of the older term instead. Then return out of the function so that the "create" hooks
$duplicate_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id, t.slug, tt.term_taxonomy_id, tt.taxonomy FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON ( tt.term_id = t.term_id ) WHERE t.slug = %s AND tt.parent = %d AND tt.taxonomy = %s AND t.term_id < %d AND tt.term_taxonomy_id != %d", $slug, $parent, $taxonomy, $term_id, $tt_id ) );
* Filters the duplicate term check that takes place during term creation.
* Term parent + taxonomy + slug combinations are meant to be unique, and wp_insert_term()
* performs a last-minute confirmation of this uniqueness before allowing a new term
* to be created. Plugins with different uniqueness requirements may use this filter
* to bypass or modify the duplicate-term check.
* @param object $duplicate_term Duplicate term row from terms table, if found.
* @param string $term Term being inserted.
* @param string $taxonomy Taxonomy name.
* @param array $args Arguments passed to wp_insert_term().
* @param int $tt_id term_taxonomy_id for the newly created term.
$duplicate_term = apply_filters( 'wp_insert_term_duplicate_term_check', $duplicate_term, $term, $taxonomy, $args, $tt_id );
$wpdb->delete( $wpdb->terms, array( 'term_id' => $term_id ) );
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
$term_id = (int) $duplicate_term->term_id;
$tt_id = (int) $duplicate_term->term_taxonomy_id;
clean_term_cache( $term_id, $taxonomy );
'term_taxonomy_id' => $tt_id,
* Fires immediately after a new term is created, before the term cache is cleaned.
* The {@see 'create_$taxonomy'} hook is also available for targeting a specific
* @since 6.1.0 The `$args` parameter was added.
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
* @param array $args Arguments passed to wp_insert_term().
do_action( 'create_term', $term_id, $tt_id, $taxonomy, $args );
* Fires after a new term is created for a specific taxonomy.
* The dynamic portion of the hook name, `$taxonomy`, refers
* to the slug of the taxonomy the term was created for.
* Possible hook names include:
* @since 6.1.0 The `$args` parameter was added.
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param array $args Arguments passed to wp_insert_term().
do_action( "create_{$taxonomy}", $term_id, $tt_id, $args );
* Filters the term ID after a new term is created.
* @since 6.1.0 The `$args` parameter was added.
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param array $args Arguments passed to wp_insert_term().
$term_id = apply_filters( 'term_id_filter', $term_id, $tt_id, $args );
clean_term_cache( $term_id, $taxonomy );
* Fires after a new term is created, and after the term cache has been cleaned.
* The {@see 'created_$taxonomy'} hook is also available for targeting a specific
* @since 6.1.0 The `$args` parameter was added.
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
* @param array $args Arguments passed to wp_insert_term().
do_action( 'created_term', $term_id, $tt_id, $taxonomy, $args );
* Fires after a new term in a specific taxonomy is created, and after the term
* cache has been cleaned.
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
* Possible hook names include:
* @since 6.1.0 The `$args` parameter was added.
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param array $args Arguments passed to wp_insert_term().
do_action( "created_{$taxonomy}", $term_id, $tt_id, $args );
* Fires after a term has been saved, and the term cache has been cleared.
* The {@see 'saved_$taxonomy'} hook is also available for targeting a specific
* @since 6.1.0 The `$args` parameter was added.
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
* @param bool $update Whether this is an existing term being updated.
* @param array $args Arguments passed to wp_insert_term().
do_action( 'saved_term', $term_id, $tt_id, $taxonomy, false, $args );
* Fires after a term in a specific taxonomy has been saved, and the term
* cache has been cleared.
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
* Possible hook names include:
* @since 6.1.0 The `$args` parameter was added.
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param bool $update Whether this is an existing term being updated.
* @param array $args Arguments passed to wp_insert_term().
do_action( "saved_{$taxonomy}", $term_id, $tt_id, false, $args );
'term_taxonomy_id' => $tt_id,
* Creates term and taxonomy relationships.
* Relates an object (post, link, etc.) to a term and taxonomy type. Creates the
* term and taxonomy relationship if it doesn't already exist. Creates a term if
* it doesn't exist (using the slug).
* A relationship means that the term is grouped in or belongs to the taxonomy.
* A term has no meaning until it is given context by defining which taxonomy it
* @global wpdb $wpdb WordPress database abstraction object.
* @param int $object_id The object to relate to.
* @param string|int|array $terms A single term slug, single term ID, or array of either term slugs or IDs.
* Will replace all existing related terms in this taxonomy. Passing an
* empty array will remove all related terms.
* @param string $taxonomy The context in which to relate the term to the object.
* @param bool $append Optional. If false will delete difference of terms. Default false.
* @return array|WP_Error Term taxonomy IDs of the affected terms or WP_Error on failure.
function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
$object_id = (int) $object_id;
if ( ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
} elseif ( ! is_array( $terms ) ) {
$terms = array( $terms );
$old_tt_ids = wp_get_object_terms(
'update_term_meta_cache' => false,
foreach ( (array) $terms as $term ) {
if ( '' === trim( $term ) ) {
$term_info = term_exists( $term, $taxonomy );
// Skip if a non-existent term ID is passed.
$term_info = wp_insert_term( $term, $taxonomy );
if ( is_wp_error( $term_info ) ) {
$tt_id = $term_info['term_taxonomy_id'];
if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) ) {
* Fires immediately before an object-term relationship is added.
* @since 4.7.0 Added the `$taxonomy` parameter.
* @param int $object_id Object ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
do_action( 'add_term_relationship', $object_id, $tt_id, $taxonomy );
$wpdb->term_relationships,
'object_id' => $object_id,
'term_taxonomy_id' => $tt_id,
* Fires immediately after an object-term relationship is added.
* @since 4.7.0 Added the `$taxonomy` parameter.
* @param int $object_id Object ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
do_action( 'added_term_relationship', $object_id, $tt_id, $taxonomy );
wp_update_term_count( $new_tt_ids, $taxonomy );
$delete_tt_ids = array_diff( $old_tt_ids, $tt_ids );
$in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'";
$delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) );
$delete_term_ids = array_map( 'intval', $delete_term_ids );
$remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy );
if ( is_wp_error( $remove ) ) {
$t = get_taxonomy( $taxonomy );
if ( ! $append && isset( $t->sort ) && $t->sort ) {
$final_tt_ids = wp_get_object_terms(
'update_term_meta_cache' => false,
foreach ( $tt_ids as $tt_id ) {
if ( in_array( (int) $tt_id, $final_tt_ids, true ) ) {
$values[] = $wpdb->prepare( '(%d, %d, %d)', $object_id, $tt_id, ++$term_order );
if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . implode( ',', $values ) . ' ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)' ) ) {
return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database.' ), $wpdb->last_error );
wp_cache_delete( $object_id, $taxonomy . '_relationships' );
wp_cache_set_terms_last_changed();
* Fires after an object's terms have been set.
* @param int $object_id Object ID.
* @param array $terms An array of object term IDs or slugs.
* @param array $tt_ids An array of term taxonomy IDs.
* @param string $taxonomy Taxonomy slug.
* @param bool $append Whether to append new terms to the old terms.
* @param array $old_tt_ids Old array of term taxonomy IDs.
do_action( 'set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids );
* Adds term(s) associated with a given object.
* @param int $object_id The ID of the object to which the terms will be added.
* @param string|int|array $terms The slug(s) or ID(s) of the term(s) to add.
* @param array|string $taxonomy Taxonomy name.
* @return array|WP_Error Term taxonomy IDs of the affected terms.
function wp_add_object_terms( $object_id, $terms, $taxonomy ) {
return wp_set_object_terms( $object_id, $terms, $taxonomy, true );
* Removes term(s) associated with a given object.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int $object_id The ID of the object from which the terms will be removed.
* @param string|int|array $terms The slug(s) or ID(s) of the term(s) to remove.
* @param string $taxonomy Taxonomy name.