* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
do_action( 'created_term', $term_id, $tt_id, $taxonomy );
* 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.
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
do_action( "created_{$taxonomy}", $term_id, $tt_id );
* 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
* @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.
do_action( 'saved_term', $term_id, $tt_id, $taxonomy, false );
* 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.
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param bool $update Whether this is an existing term being updated.
do_action( "saved_{$taxonomy}", $term_id, $tt_id, false );
'term_taxonomy_id' => $tt_id,
* Create 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 value 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.' ) );
if ( ! 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 ) ) {
$term_ids[] = $term_info['term_id'];
$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_delete( 'last_changed', 'terms' );
* Fires after an object's terms have been set.
* @param int $object_id Object ID.
* @param array $terms An array of object terms.
* @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 );
* Add 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 );
* Remove 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 array|string $taxonomy Taxonomy name.
* @return bool|WP_Error True on success, false or WP_Error on failure.
function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
$object_id = (int) $object_id;
if ( ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
if ( ! is_array( $terms ) ) {
$terms = array( $terms );
foreach ( (array) $terms as $term ) {
if ( '' === trim( $term ) ) {
$term_info = term_exists( $term, $taxonomy );
// Skip if a non-existent term ID is passed.
if ( is_wp_error( $term_info ) ) {
$tt_ids[] = $term_info['term_taxonomy_id'];
$in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'";
* Fires immediately before an object-term relationship is deleted.
* @since 4.7.0 Added the `$taxonomy` parameter.
* @param int $object_id Object ID.
* @param array $tt_ids An array of term taxonomy IDs.
* @param string $taxonomy Taxonomy slug.
do_action( 'delete_term_relationships', $object_id, $tt_ids, $taxonomy );
$deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
wp_cache_delete( $object_id, $taxonomy . '_relationships' );
wp_cache_delete( 'last_changed', 'terms' );
* Fires immediately after an object-term relationship is deleted.
* @since 4.7.0 Added the `$taxonomy` parameter.
* @param int $object_id Object ID.
* @param array $tt_ids An array of term taxonomy IDs.
* @param string $taxonomy Taxonomy slug.
do_action( 'deleted_term_relationships', $object_id, $tt_ids, $taxonomy );
wp_update_term_count( $tt_ids, $taxonomy );
* Will make slug unique, if it isn't already.
* The `$slug` has to be unique global to every taxonomy, meaning that one
* taxonomy term can't have a matching slug with another taxonomy term. Each
* slug has to be globally unique for every taxonomy.
* The way this works is that if the taxonomy that the term belongs to is
* hierarchical and has a parent, it will append that parent to the $slug.
* If that still doesn't return a unique slug, then it tries to append a number
* until it finds a number that is truly unique.
* The only purpose for `$term` is for appending a parent, if one exists.
* @global wpdb $wpdb WordPress database abstraction object.
* @param string $slug The string that will be tried for a unique slug.
* @param object $term The term object that the `$slug` will belong to.
* @return string Will return a true unique slug.
function wp_unique_term_slug( $slug, $term ) {
// As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.
if ( ! term_exists( $slug ) || get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {
* If the taxonomy supports hierarchy and the term has a parent, make the slug unique
* by incorporating parent slugs.
if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) {
$the_parent = $term->parent;
while ( ! empty( $the_parent ) ) {
$parent_term = get_term( $the_parent, $term->taxonomy );
if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) {
$parent_suffix .= '-' . $parent_term->slug;
if ( ! term_exists( $slug . $parent_suffix ) ) {
if ( empty( $parent_term->parent ) ) {
$the_parent = $parent_term->parent;
// If we didn't get a unique slug, try appending a number to make it unique.
* Filters whether the proposed unique term slug is bad.
* @param bool $needs_suffix Whether the slug needs to be made unique with a suffix.
* @param string $slug The slug.
* @param object $term Term object.
if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) {
if ( ! empty( $term->term_id ) ) {
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
if ( $wpdb->get_var( $query ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$alt_slug = $slug . "-$num";
$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
* Filters the unique term slug.
* @param string $slug Unique term slug.
* @param object $term Term object.
* @param string $original_slug Slug originally passed to the function for testing.
return apply_filters( 'wp_unique_term_slug', $slug, $term, $original_slug );
* Update term based on arguments provided.
* The `$args` will indiscriminately override all values with the same field name.
* Care must be taken to not override important information need to update or
* update will fail (or perhaps create a new term, neither would be acceptable).
* Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not
* defined in `$args` already.
* 'alias_of' will create a term group, if it doesn't already exist, and
* update it for the `$term`.
* If the 'slug' argument in `$args` is missing, then the 'name' will be used.
* If you set 'slug' and it isn't unique, then a WP_Error is returned.
* If you don't pass any slug, then a unique one will be created.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int $term_id The ID of the term.
* @param string $taxonomy The taxonomy of the term.
* @param array|string $args {
* Optional. Array or string of arguments for updating a term.
* @type string $alias_of Slug of the term to make this term an alias of.
* Default empty string. Accepts a term slug.
* @type string $description The term description. Default empty string.
* @type int $parent The id of the parent term. Default 0.
* @type string $slug The term slug to use. Default empty string.
* @return array|WP_Error An array containing the `term_id` and `term_taxonomy_id`,
function wp_update_term( $term_id, $taxonomy, $args = array() ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
$term_id = (int) $term_id;
// First, get all of the original args.
$term = get_term( $term_id, $taxonomy );
if ( is_wp_error( $term ) ) {
return new WP_Error( 'invalid_term', __( 'Empty Term.' ) );