A WordPress-centric search engine for devs and theme authors



wp_remove_object_terms ›

Since3.6.0
Deprecatedn/a
wp_remove_object_terms ( $object_id, $terms, $taxonomy )
Parameters: (3)
  • (int) $object_id The ID of the object from which the terms will be removed.
    Required: Yes
  • (string|int|array) $terms The slug(s) or ID(s) of the term(s) to remove.
    Required: Yes
  • (array|string) $taxonomy Taxonomy name.
    Required: Yes
Returns:
  • (bool|WP_Error) True on success, false or WP_Error on failure.
Defined at:
Codex:

Remove term(s) associated with a given object.



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.' ) );
	}

	$taxonomy_object = get_taxonomy( $taxonomy );

	$object_types = (array) $taxonomy_object->object_type;
	foreach ( $object_types as &$object_type ) {
		if ( 0 === strpos( $object_type, 'attachment:' ) ) {
			list( $object_type ) = explode( ':', $object_type );
		}
	}

	if ( array_filter( $object_types, 'post_type_exists' ) !== $object_types ) {
		// This taxonomy applies to non-posts, count changes now.
		$do_recount = ! _wp_prevent_term_counting();
	} elseif (
		'publish' === get_post_status( $object_id ) ||
		(
			'inherit' === get_post_status( $object_id ) &&
			'publish' === get_post_status( wp_get_post_parent_id( $object_id ) )
		)
	) {
		// Published post, count changes now.
		$do_recount = ! _wp_prevent_term_counting();
	} else {
		$do_recount = false;
	}

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

		if ( $do_recount ) {
			wp_decrement_term_count( $tt_ids, $taxonomy );
		}

		return (bool) $deleted;
	}

	return false;
}