wp_remove_object_terms( int $object_id, string|int|array $terms, string $taxonomy ): bool|WP_Error
- Since
- 3.6.0
- Source
wp-includes/taxonomy.php:2997
Compatibility
- WordPress
- since 3.6.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 ID of the object from which the terms will be removed.
$termsstring|int|array- The slug(s) or ID(s) of the term(s) to remove.
$taxonomystring- Taxonomy name.
Return value
bool|WP_Error- True on success, false or WP_Error on failure.
Performance profile
How much work a call to wp_remove_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
- 18–62
- Plugin surface
- 2 hooks
- Called by
- 3
Reaches the database via ->query().
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 92.
Third-party callbacks on 'delete_term_relationships', 'deleted_term_relationships' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - cacheobject cache
wp_cache_delete()called directly - sqldatabase query
->query()called directly - querycontent query
get_terms()one call below wp_remove_object_terms()
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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_remove_object_terms() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
taxonomy_exists() | 18–21 | taxonomy_exists() |
!taxonomy_exists() | 18 | taxonomy_exists(), __() |
taxonomy_exists() && !$terms && is_wp_error() | 31–35 | taxonomy_exists(), term_exists(), is_wp_error() |
taxonomy_exists() | 59–62 | taxonomy_exists(), do_action(), ->prepare(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action(), wp_update_term_count() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 92 | 18–62 | 9 | |
| 8.5 | 92 | 18–62 | 9 | |
| 8.4 | 92 | 18–62 | 9 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 97 | 18–65 | 9 | |
| 8.2 | 97 | 18–65 | 9 | |
| 8.1 | 97 | 18–65 | 9 | |
| 7.4 | 97 | 18–65 | 9 |
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 · 2
2 hooks fire while wp_remove_object_terms() runs, in this order:
- do_action( delete_term_relationships )actionline 3045 (+48 into the body)
Fires immediately before an object-term relationship is deleted.
- do_action( deleted_term_relationships )actionline 3062 (+65 into the body)
Fires immediately after an object-term relationship is deleted.
Uses · 9
- taxonomy_exists()Determines whether the taxonomy name exists.
- __()Retrieves the translation of $text.
- term_exists()Determines whether a taxonomy term exists.
- 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_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.
- wp_update_term_count()Updates the amount of terms in taxonomy.
- WP_Error::__construct()Initializes the error.
Used by · 3
- wp_delete_object_term_relationships()Unlinks the object from the taxonomy or taxonomies.
- wp_delete_term()Removes a term from the database.
- wp_set_object_terms()Creates term and taxonomy relationships.
Source code
function wp_remove_object_terms( $object_id, $terms, $taxonomy ) { global $wpdb; $object_id = (int) $object_id; if ( ! taxonomy_exists( $taxonomy ) ) { return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); } if ( ! is_array( $terms ) ) { $terms = array( $terms ); } $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; } } if ( is_wp_error( $term_info ) ) { return $term_info; } $tt_ids[] = $term_info['term_taxonomy_id']; } if ( $tt_ids ) { $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'"; /** * Fires immediately before an object-term relationship is deleted. * * @since 2.9.0 * @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_set_terms_last_changed(); /** * Fires immediately after an object-term relationship is deleted. * * @since 2.9.0 * @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 ); return (bool) $deleted; } return false;}Changelog
Introduced in 3.6.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.