Make WordPress Core

Ticket #15475: wp_unset_object_terms-101118-1.diff

File wp_unset_object_terms-101118-1.diff, 2.8 KB (added by simonwheatley, 15 years ago)

Adds wp_unset_object_terms

  • Users/simon/Projects/WordPress-Bleeding/site/wp-includes/taxonomy.php

     
    14681468}
    14691469
    14701470/**
    1471  * Will unlink the term from the taxonomy.
     1471 * Will unlink the object from the taxonomy or taxonomies.
    14721472 *
    1473  * Will remove the term's relationship to the taxonomy, not the term or taxonomy
    1474  * itself. The term and taxonomy will still exist. Will require the term's
    1475  * object ID to perform the operation.
     1473 * Will remove all relationships between the object and any terms in
     1474 * a particular taxonomy or taxonomies. Does not remove the term or
     1475 * taxonomy itself.
    14761476 *
    14771477 * @package WordPress
    14781478 * @subpackage Taxonomy
     
    19871987}
    19881988
    19891989/**
     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 **/
     2002function 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/**
    19902033 * Will make slug unique, if it isn't already.
    19912034 *
    19922035 * The $slug has to be unique global to every taxonomy, meaning that one