| 1990 | * Unrelates the object(s) from the specified term(s). |
| 1991 | * |
| 1992 | * @package WordPress |
| 1993 | * @subpackage Taxonomy |
| 1994 | * @since 2.3.1 |
| 1995 | * @uses wp_update_term_count To update the term counts after the relationships have been removed |
| 1996 | * |
| 1997 | * @param integer|array $object_ids Either an integer, or an array of object ids to be unrelated from the terms |
| 1998 | * @param integer|array $term_ids Either an integer, or an array of term ids to be unrelated from the objects |
| 1999 | * @param string $taxonomy The taxonomy for the term |
| 2000 | * @return void |
| 2001 | **/ |
| 2002 | function wp_unset_object_terms( $object_ids, $term_ids, $taxonomy ) { |
| 2003 | global $wpdb; |
| 2004 | |
| 2005 | if ( ! is_array( $object_ids ) ) |
| 2006 | $object_ids = (array) $object_ids; |
| 2007 | if ( ! is_array( $term_ids ) ) |
| 2008 | $term_ids = (array) $term_ids; |
| 2009 | |
| 2010 | // N.B. get_terms will sanitise the $term_ids |
| 2011 | $terms = get_terms( $taxonomy, array( 'include' => $term_ids ) ); |
| 2012 | $tt_ids = array(); |
| 2013 | // Note: There may be some way to use array_uintersect_uassoc to extract |
| 2014 | // the $tt_ids without using a loop, but sadly it's PHP5 only. |
| 2015 | foreach ( $terms as $term ) |
| 2016 | $tt_ids[] = $term->term_taxonomy_id; |
| 2017 | $in_tt_ids = implode( ',', $tt_ids ); |
| 2018 | |
| 2019 | // Sanitise the $object_ids, goodness knows where they've been |
| 2020 | array_walk( $object_ids, create_function( '& $id', '$id = intval( $id );' ) ); |
| 2021 | $in_object_ids = implode( ',', $object_ids ); |
| 2022 | |
| 2023 | foreach ( $object_ids as $object_id ) |
| 2024 | do_action( 'delete_term_relationships', $object_id, $tt_ids ); |
| 2025 | // Note: Preparing is pointless in this case, but the inputs are sanitary |
| 2026 | $wpdb->query( "DELETE FROM $wpdb->term_relationships WHERE object_id IN ( $in_object_ids ) AND term_taxonomy_id IN ($in_tt_ids)" ); |
| 2027 | foreach ( $object_ids as $object_id ) |
| 2028 | do_action( 'deleted_term_relationships', $object_id, $tt_ids ); |
| 2029 | wp_update_term_count( $tt_ids, $taxonomy ); |
| 2030 | } |
| 2031 | |
| 2032 | /** |