[ Web Proxy ]
URL:
Viewing: https://developer.wordpress.org/reference/functions/wp_remove_object_terms/ [Back]  [Original]

wp_remove_object_terms() Function | Developer.WordPress.org Skip to content
WordPress.org

wp_remove_object_terms( int $object_id, string|int|array $terms, string $taxonomy ): bool|WP_Error

Removes term(s) associated with a given object.

Parameters

$object_idintrequired
The ID of the object from which the terms will be removed.
$termsstring|int|arrayrequired
The slug(s) or ID(s) of the term(s) to remove.
$taxonomystringrequired
Taxonomy name.

Return

bool|WP_Error True on success, false or WP_Error on failure.

Source

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;
}

Hooks

do_action( ‘deleted_term_relationships’, int $object_id, array $tt_ids, string $taxonomy )

Fires immediately after an object-term relationship is deleted.

do_action( ‘delete_term_relationships’, int $object_id, array $tt_ids, string $taxonomy )

Fires immediately before an object-term relationship is deleted.

UsesDescription
wp_cache_set_terms_last_changed()wp-includes/taxonomy.php

Sets the last changed time for the ‘terms’ cache group.

wp_cache_delete()wp-includes/cache.php

Removes the cache contents matching key and group.

wp_update_term_count()wp-includes/taxonomy.php

Updates the amount of terms in taxonomy.

term_exists()wp-includes/taxonomy.php

Determines whether a taxonomy term exists.

taxonomy_exists()wp-includes/taxonomy.php

Determines whether the taxonomy name exists.

wpdb::query()wp-includes/class-wpdb.php

Performs a database query, using current database connection.

__()wp-includes/l10n.php

Retrieves the translation of $text.

do_action()wp-includes/plugin.php

Calls the callback functions that have been added to an action hook.

wpdb::prepare()wp-includes/class-wpdb.php

Prepares a SQL query for safe execution.

is_wp_error()wp-includes/load.php

Checks whether the given variable is a WordPress Error.

WP_Error::__construct()wp-includes/class-wp-error.php

Initializes the error.

Show 6 moreShow less
Used byDescription
wp_set_object_terms()wp-includes/taxonomy.php

Creates term and taxonomy relationships.

wp_delete_object_term_relationships()wp-includes/taxonomy.php

Unlinks the object from the taxonomy or taxonomies.

wp_delete_term()wp-includes/taxonomy.php

Removes a term from the database.

Changelog

VersionDescription
3.6.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Example

    Remove tag from post.

    wp_remove_object_terms( $post_id, 'sweet', 'post_tag' );
    Log in to add feedback
  2. Skip to note 4 content

    Even though it’s not specified, I found that $terms can also be the names of terms.

    $post_tags = array(
       'Tag name',
       'Another tag name',
    );
    
    wp_remove_object_terms( $post_id, $post_tags, 'post_tag' );
    Log in to add feedback

You must log in before being able to contribute a note or feedback.


Web Proxy Viewer  |  New URL  |  Original Page