wp_remove_object_terms
Remove term(s) associated with a given object.
Description
(bool|WP_Error) wp_remove_object_terms( (int) $object_id, (array|int|string) $terms, (array|string) $taxonomy );
Returns (bool|WP_Error)
True on success, false or WP_Error on failure.
Parameters (3)
- 0. $object_id (int)
- The ID of the object from which the terms will be removed.
- 1. $terms (array|int|string)
- The slug(s) or ID(s) of the term(s) to remove.
- 2. $taxonomy (array|string)
- Taxonomy name.
Usage
if ( !function_exists( 'wp_remove_object_terms' ) ) { require_once ABSPATH . WPINC . '/taxonomy.php'; } // The ID of the object from which the terms will be removed. $object_id = -1; // The slug(s) or ID(s) of the term(s) to remove. $terms = null; // Taxonomy name. $taxonomy = null; // NOTICE! Understand what this does before running. $result = wp_remove_object_terms($object_id, $terms, $taxonomy);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/taxonomy.php
- function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
- global $wpdb;
- $object_id = (int) $object_id;
- if ( ! taxonomy_exists( $taxonomy ) ) {
- }
- if ( ! is_array( $terms ) ) {
- $terms = array( $terms );
- }
- $tt_ids = array();
- foreach ( (array) $terms as $term ) {
- if ( ! strlen( trim( $term ) ) ) {
- continue;
- }
- if ( ! $term_info = term_exists( $term, $taxonomy ) ) {
- // 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' );
- /**
- * 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;
- }