wp_delete_object_term_relationships

Definition:
function wp_delete_object_term_relationships( $object_id, $taxonomies ) {}

Will unlink the object from the taxonomy or taxonomies.
Will remove all relationships between the object and any terms in a particular taxonomy or taxonomies. Does not remove the term or taxonomy itself.

Parameters

  • int $object_id: The term Object Id that refers to the term
  • string|array $taxonomies: List of Taxonomy Names or single Taxonomy name.

Defined actions

  • delete_term_relationships
    do_action( 'delete_term_relationships', $object_id, $tt_ids );
  • deleted_term_relationships
    do_action( 'deleted_term_relationships', $object_id, $tt_ids );

Source code

function wp_delete_object_term_relationships( $object_id, $taxonomies ) {

	global $wpdb;



	$object_id = (int) $object_id;



	if ( !is_array($taxonomies) )

		$taxonomies = array($taxonomies);



	foreach ( (array) $taxonomies as $taxonomy ) {

		$tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));

		$in_tt_ids = "'" . implode("', '", $tt_ids) . "'";

		do_action( 'delete_term_relationships', $object_id, $tt_ids );

		$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) );

		do_action( 'deleted_term_relationships', $object_id, $tt_ids );

		wp_update_term_count($tt_ids, $taxonomy);

	}

}

3593

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: