• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / wp_remove_object_terms() – Remove term(s) associated with a given object.

You appear to be a bot. Output may be restricted

Description

Remove term(s) associated with a given object.

Usage

$bool|WP_Error = wp_remove_object_terms( $object_id, $terms, $taxonomy );

Parameters

$object_id
( int ) required – The ID of the object from which the terms will be removed.
$terms
( string|int|array ) required – The slug(s) or ID(s) of the term(s) to remove.
$taxonomy
( array|string ) required – Taxonomy name.

Returns

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

Source

File name: wordpress/wp-includes/taxonomy.php
Lines:

1 to 74 of 74
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_delete( 'last_changed', 'terms' );

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

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: wp_remove_object_terms
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/taxonomy.php
File ref: wp-includes/taxonomy.php
Deprecated?: No
API Letters: O,R,T,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites