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
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
- Scaling
- Scales with input
- Instructions
- 19–125
- Plugin surface
- 3 hooks
- Called by
- 8
Reaches the database via ->get_col().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 224.
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.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
wp_get_object_terms()called directly - hookthird-party callbacks
do_action()called directly - cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
!taxonomy_exists() | 19 | taxonomy_exists(), __() |
taxonomy_exists() && !$terms && is_wp_error() | 37–39 | taxonomy_exists(), term_exists(), is_wp_error() |
taxonomy_exists() && !$terms && is_wp_error() | 43–45 | taxonomy_exists(), wp_get_object_terms(), term_exists(), is_wp_error() |
taxonomy_exists() && !$terms && !is_int($term) && is_wp_error() | 44–46 | taxonomy_exists(), term_exists(), wp_insert_term(), is_wp_error() |
taxonomy_exists() | 46–53 | taxonomy_exists(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action() |
taxonomy_exists() && !$terms && !is_int($term) && is_wp_error() | 50–52 | taxonomy_exists(), wp_get_object_terms(), term_exists(), wp_insert_term(), is_wp_error() |
taxonomy_exists() | 50–57 | taxonomy_exists(), wp_update_term_count(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action() |
taxonomy_exists() | 52–59 | taxonomy_exists(), array_diff(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action() |
taxonomy_exists() | 52–59 | taxonomy_exists(), wp_get_object_terms(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action() |
taxonomy_exists() | 56–63 | taxonomy_exists(), wp_update_term_count(), array_diff(), get_taxonomy(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action() |
taxonomy_exists() | 56–63 | taxonomy_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–65 | taxonomy_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) && $t | 61–65 | taxonomy_exists(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action() |
taxonomy_exists() | 62–69 | taxonomy_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–66 | taxonomy_exists(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error() |
taxonomy_exists() && isset($t) && $t | 65–69 | taxonomy_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) && $t | 67–71 | taxonomy_exists(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert() |
taxonomy_exists() && isset($t) && $t | 67–71 | taxonomy_exists(), array_diff(), get_taxonomy(), wp_get_object_terms(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action() |
taxonomy_exists() && isset($t) && $t | 67–71 | taxonomy_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–70 | taxonomy_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–72 | taxonomy_exists(), wp_get_object_terms(), array_diff(), ->prepare(), ->get_col(), array_map(), wp_remove_object_terms(), is_wp_error() |
taxonomy_exists() && isset($t) && $t | 71–75 | taxonomy_exists(), wp_update_term_count(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert() |
taxonomy_exists() && isset($t) && $t | 71–75 | taxonomy_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) && $t | 71–75 | taxonomy_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) && $t | 73–77 | taxonomy_exists(), get_taxonomy(), wp_get_object_terms(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action() |
taxonomy_exists() && isset($t) && $t | 73–77 | taxonomy_exists(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert() |
taxonomy_exists() && isset($t) && $t | 73–77 | taxonomy_exists(), wp_get_object_terms(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert() |
taxonomy_exists() && isset($t) && $t | 73–77 | taxonomy_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–76 | taxonomy_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) && $t | 77–81 | taxonomy_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) && $t | 77–81 | taxonomy_exists(), wp_update_term_count(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert() |
taxonomy_exists() && isset($t) && $t | 77–81 | taxonomy_exists(), wp_get_object_terms(), wp_update_term_count(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert() |
taxonomy_exists() && isset($t) && $t | 77–81 | taxonomy_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) && $t | 79–83 | taxonomy_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) && $t | 79–83 | taxonomy_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) && $t | 79–83 | taxonomy_exists(), wp_get_object_terms(), array_diff(), get_taxonomy(), wp_get_object_terms(), ->query(), __(), ->insert() |
taxonomy_exists() && isset($t) && $t | 83–87 | taxonomy_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) && $t | 83–87 | taxonomy_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) && $t | 83–87 | taxonomy_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–91 | taxonomy_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) && $t | 85–89 | taxonomy_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–95 | taxonomy_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) && $t | 89–93 | taxonomy_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–97 | taxonomy_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–101 | taxonomy_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) && $t | 99–103 | taxonomy_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) && $t | 103–107 | taxonomy_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) && $t | 105–109 | taxonomy_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) && $t | 105–109 | taxonomy_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) && $t | 109–113 | taxonomy_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) && $t | 109–113 | taxonomy_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) && $t | 111–115 | taxonomy_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) && $t | 111–115 | taxonomy_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) && $t | 115–119 | taxonomy_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) && $t | 115–119 | taxonomy_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) && $t | 117–121 | taxonomy_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) && $t | 121–125 | taxonomy_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 224 | 19–125 | 23 | |
| 8.5 | 224 | 19–125 | 23 | |
| 8.4 | 224 | 19–125 | 23 | 11 fewer instructions than PHP 8.3 |
| 8.3 | 235 | 19–131 | 23 | |
| 8.2 | 235 | 19–131 | 23 | |
| 8.1 | 235 | 19–131 | 23 | |
| 7.4 | 235 | 19–131 | 23 |
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:
- do_action( add_term_relationship )actionline 2879 (+69 into the body)
Fires immediately before an object-term relationship is added.
- do_action( added_term_relationship )actionline 2899 (+89 into the body)
Fires immediately after an object-term relationship is added.
- do_action( set_object_terms )actionline 2966 (+156 into the body)
Fires after an object's terms have been set.
Uses · 13
- taxonomy_exists()Determines whether the taxonomy name exists.
- __()Retrieves the translation of $text.
- wp_get_object_terms()Retrieves the terms associated with the given object(s), in the supplied taxonomies.
- term_exists()Determines whether a taxonomy term exists.
- wp_insert_term()Adds a new term to the database.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_update_term_count()Updates the amount of terms in taxonomy.
- wp_remove_object_terms()Removes term(s) associated with a given object.
- get_taxonomy()Retrieves the taxonomy object of $taxonomy.
- wp_cache_delete()Removes the cache contents matching key and group.
- wp_cache_set_terms_last_changed()Sets the last changed time for the 'terms' cache group.
Show all 13
- WP_Error::__construct()Initializes the error.
Used by · 8
- WP_REST_Posts_Controller::handle_terms()Updates the post's terms from a REST request.
- media_upload_form_handler()Handles form submissions for the legacy media uploader.
- wp_add_object_terms()Adds term(s) associated with a given object.
- wp_ajax_save_attachment_compat()Handles saving backward compatible attachment attributes via AJAX.
- wp_delete_term()Removes a term from the database.
- wp_set_link_cats()Updates link with the specified link categories.
- wp_set_post_terms()Sets the terms for a post.
- wp_update_nav_menu_item()Saves the properties of a menu item or create a new one.
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.
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.