wppaste
WordPress

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:3003
Removes term(s) associated with a given object.

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

Reaches the database via ->query().

Scaling
Scales with input

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

Instructions
18–62

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

Plugin surface
2 hooks

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

Called by
3

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

What it touches

  • hookthird-party callbacksdo_action()called directly
  • cacheobject cachewp_cache_delete()called directly
  • sqldatabase query->query()called directly
  • querycontent queryget_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.

WhenInstructionsCalls it makes
taxonomy_exists()18–21taxonomy_exists()
!taxonomy_exists()18taxonomy_exists(), __()
taxonomy_exists() && !$terms && is_wp_error()31–35taxonomy_exists(), term_exists(), is_wp_error()
taxonomy_exists()59–62taxonomy_exists(), do_action(), ->prepare(), ->query(), wp_cache_delete(), wp_cache_set_terms_last_changed(), do_action(), wp_update_term_count()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9218–629
8.59218–629
8.49218–6295 fewer instructions than PHP 8.3
8.39718–659
8.29718–659
8.19718–659
7.49718–659

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:

  1. do_action( delete_term_relationships )actionline 3051 (+48 into the body)

    Fires immediately before an object-term relationship is deleted.

  2. do_action( deleted_term_relationships )actionline 3068 (+65 into the body)

    Fires immediately after an object-term relationship is deleted.

Uses · 9

Used by · 3

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.

  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.9.7 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.