wppaste
WordPress

wp_set_object_terms( int $object_id, string|int|array $terms, string $taxonomy, bool $append = false ): array|WP_Error

Since
2.3.0
Source
wp-includes/taxonomy.php:2810
Creates term and taxonomy relationships.

Description

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 exists under.

Compatibility

WordPress
since 2.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$object_idint
The object to relate to.
$termsstring|int|array
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.
$taxonomystring
The context in which to relate the term to the object.
$appendbooloptional
If false will delete difference of terms. Default false.Default: false

Return value

array|WP_Error
Term taxonomy IDs of the affected terms or WP_Error on failure.

Performance profile

How much work a call to wp_set_object_terms() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_col().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
19–125

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 224.

Plugin surface
3 hooks

Third-party callbacks on 'add_term_relationship', 'added_term_relationship', 'set_object_terms' run inside this call, and their cost is not bounded by anything here.

Called by
8

8 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent querywp_get_object_terms()called directly
  • hookthird-party callbacksdo_action()called directly
  • cacheobject cachewp_cache_delete()called directly
  • sqldatabase query->get_col()called directly

Further down the call graph this can also reach option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 57 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_set_object_terms() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!taxonomy_exists()19taxonomy_exists(), __()
taxonomy_exists() && !$terms && is_wp_error()37–39taxonomy_exists(), term_exists(), is_wp_error()
taxonomy_exists() && !$terms && is_wp_error()43–45taxonomy_exists(), wp_get_object_terms(), term_exists(), is_wp_error()
taxonomy_exists() && !$terms && !is_int($term) && is_wp_error()44–46taxonomy_exists(), term_exists(), wp_insert_term(), is_wp_error()
taxonomy_exists()46–53taxonomy_exists(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !$terms && !is_int($term) && is_wp_error()50–52taxonomy_exists(), wp_get_object_terms(), term_exists(), wp_insert_term(), is_wp_error()
taxonomy_exists()50–57taxonomy_exists(), wp_update_term_count(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists()52–59taxonomy_exists(), array_diff(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists()52–59taxonomy_exists(), wp_get_object_terms(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists()56–63taxonomy_exists(), wp_update_term_count(), array_diff(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists()56–63taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists()58–65taxonomy_exists(), wp_get_object_terms(), array_diff(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
45 further outcomes, up to 125 instructions
taxonomy_exists() && isset($t) && $t61–65taxonomy_exists(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists()62–69taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), array_diff(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && is_wp_error()63–66taxonomy_exists(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error()
taxonomy_exists() && isset($t) && $t65–69taxonomy_exists(), wp_update_term_count(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t67–71taxonomy_exists(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && isset($t) && $t67–71taxonomy_exists(), array_diff(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t67–71taxonomy_exists(), wp_get_object_terms(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && is_wp_error()67–70taxonomy_exists(), wp_update_term_count(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error()
taxonomy_exists() && is_wp_error()69–72taxonomy_exists(), wp_get_object_terms(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error()
taxonomy_exists() && isset($t) && $t71–75taxonomy_exists(), wp_update_term_count(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && isset($t) && $t71–75taxonomy_exists(), wp_update_term_count(), array_diff(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t71–75taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t73–77taxonomy_exists(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t73–77taxonomy_exists(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && isset($t) && $t73–77taxonomy_exists(), wp_get_object_terms(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && isset($t) && $t73–77taxonomy_exists(), wp_get_object_terms(), array_diff(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && is_wp_error()73–76taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error()
taxonomy_exists() && isset($t) && $t77–81taxonomy_exists(), wp_update_term_count(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t77–81taxonomy_exists(), wp_update_term_count(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && isset($t) && $t77–81taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && isset($t) && $t77–81taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), array_diff(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t79–83taxonomy_exists(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t79–83taxonomy_exists(), wp_get_object_terms(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t79–83taxonomy_exists(), wp_get_object_terms(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && isset($t) && $t83–87taxonomy_exists(), wp_update_term_count(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t83–87taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t83–87taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && !is_wp_error()84–91taxonomy_exists(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t85–89taxonomy_exists(), wp_get_object_terms(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error()88–95taxonomy_exists(), wp_update_term_count(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && isset($t) && $t89–93taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error()90–97taxonomy_exists(), wp_get_object_terms(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error()94–101taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error() && isset($t) && $t99–103taxonomy_exists(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error() && isset($t) && $t103–107taxonomy_exists(), wp_update_term_count(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error() && isset($t) && $t105–109taxonomy_exists(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && !is_wp_error() && isset($t) && $t105–109taxonomy_exists(), wp_get_object_terms(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error() && isset($t) && $t109–113taxonomy_exists(), wp_update_term_count(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && !is_wp_error() && isset($t) && $t109–113taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error() && isset($t) && $t111–115taxonomy_exists(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error() && isset($t) && $t111–115taxonomy_exists(), wp_get_object_terms(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && !is_wp_error() && isset($t) && $t115–119taxonomy_exists(), wp_update_term_count(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error() && isset($t) && $t115–119taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert()
taxonomy_exists() && !is_wp_error() && isset($t) && $t117–121taxonomy_exists(), wp_get_object_terms(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()
taxonomy_exists() && !is_wp_error() && isset($t) && $t121–125taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev22419–12523
8.522419–12523
8.422419–1252311 fewer instructions than PHP 8.3
8.323519–13123
8.223519–13123
8.123519–13123
7.423519–13123

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while wp_set_object_terms() runs, in this order:

  1. do_action( add_term_relationship )actionline 2879 (+69 into the body)

    Fires immediately before an object-term relationship is added.

  2. do_action( added_term_relationship )actionline 2899 (+89 into the body)

    Fires immediately after an object-term relationship is added.

  3. do_action( set_object_terms )actionline 2966 (+156 into the body)

    Fires after an object's terms have been set.

Uses · 13

Show all 13

Used by · 8

Source code

function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {	global $wpdb; 	$object_id = (int) $object_id; 	if ( ! taxonomy_exists( $taxonomy ) ) {		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );	} 	if ( empty( $terms ) ) {		$terms = array();	} elseif ( ! is_array( $terms ) ) {		$terms = array( $terms );	} 	if ( ! $append ) {		$old_tt_ids = wp_get_object_terms(			$object_id,			$taxonomy,			array(				'fields'                 => 'tt_ids',				'orderby'                => 'none',				'update_term_meta_cache' => false,			)		);	} else {		$old_tt_ids = array();	} 	$tt_ids     = array();	$new_tt_ids = array(); 	foreach ( (array) $terms as $term ) {		if ( '' === trim( $term ) ) {			continue;		} 		$term_info = term_exists( $term, $taxonomy ); 		if ( ! $term_info ) {			// Skip if a non-existent term ID is passed.			if ( is_int( $term ) ) {				continue;			} 			$term_info = wp_insert_term( $term, $taxonomy );		} 		if ( is_wp_error( $term_info ) ) {			return $term_info;		} 		$tt_id    = $term_info['term_taxonomy_id'];		$tt_ids[] = $tt_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 ) ) ) {			continue;		} 		/**		 * Fires immediately before an object-term relationship is added.		 *		 * @since 2.9.0		 * @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->insert(			$wpdb->term_relationships,			array(				'object_id'        => $object_id,				'term_taxonomy_id' => $tt_id,			)		); 		/**

Changelog

Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.