WordPress.org

Make WordPress Core

Ticket #15675: add-parent-arg-to-wp-get-object-terms.diff

File add-parent-arg-to-wp-get-object-terms.diff, 6.0 KB (added by mikeschinkel, 7 years ago)

Adds a "parent" arg to wp_get_object_terms(), fixes the returning of duplicate terms and adds the options id=>name and id=>term for fields.

  • taxonomy.php

     
    14981498/**
    14991499 * Will unlink the object from the taxonomy or taxonomies.
    15001500 *
    1501  * Will remove all relationships between the object and any terms in 
    1502  * a particular taxonomy or taxonomies. Does not remove the term or 
     1501 * Will remove all relationships between the object and any terms in
     1502 * a particular taxonomy or taxonomies. Does not remove the term or
    15031503 * taxonomy itself.
    15041504 *
    15051505 * @package WordPress
     
    16131613        // Clean the relationship caches for all object types using this term
    16141614        $tax_object = get_taxonomy( $taxonomy );
    16151615        foreach ( $tax_object->object_type as $object_type )
    1616                 clean_object_term_cache( $objects, $object_type ); 
    1617        
     1616                clean_object_term_cache( $objects, $object_type );
     1617
    16181618        do_action( 'delete_term_taxonomy', $tt_id );
    16191619        $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) );
    16201620        do_action( 'deleted_term_taxonomy', $tt_id );
     
    16491649 * Retrieves the terms associated with the given object(s), in the supplied taxonomies.
    16501650 *
    16511651 * The following information has to do the $args parameter and for what can be
    1652  * contained in the string or array of that parameter, if it exists.
     1652 * contained in the string or array of that parameter, if it exists. The arguments
     1653 * can be:
    16531654 *
    1654  * The first argument is called, 'orderby' and has the default value of 'name'.
    1655  * The other value that is supported is 'count'.
     1655 *  'orderby' The default value is 'name', and the other potential values are
     1656 *            'count', 'slug', 'term_group','term_order' or 'none'.
    16561657 *
    1657  * The second argument is called, 'order' and has the default value of 'ASC'.
    1658  * The only other value that will be acceptable is 'DESC'.
     1658 *  'order'   The default value of 'ASC', and the other acceptable value is 'DESC'.
    16591659 *
    1660  * The final argument supported is called, 'fields' and has the default value of
    1661  * 'all'. There are multiple other options that can be used instead. Supported
    1662  * values are as follows: 'all', 'ids', 'names', and finally
    1663  * 'all_with_object_id'.
     1660 *  'parent'  There is no default value but if specified it should contain an integer
     1661 *            value corresponding to the value of the parent field to filter by.
    16641662 *
    1665  * The fields argument also decides what will be returned. If 'all' or
    1666  * 'all_with_object_id' is choosen or the default kept intact, then all matching
    1667  * terms objects will be returned. If either 'ids' or 'names' is used, then an
    1668  * array of all matching term ids or term names will be returned respectively.
     1663 *  'fields'  The default value is 'all' with the other options supported being
     1664 *            'all', 'ids', 'names', 'id=>name', 'id=>term', 'tt_ids' and
     1665 *            'all_with_object_id'.
    16691666 *
     1667 * The fields argument also decides what will be returned. If the default is
     1668 * used or either 'all' or 'all_with_object_id' is specified then all matching
     1669 * terms objects will be returned. If either 'ids', 'tt_ids' or 'names' is used,
     1670 * then an array of all matching term ids, undocumented (tt_ids) or term names
     1671 * will be returned respectively.
     1672 *
     1673 * May return duplicate terms when when 'fields' equals 'all_with_object_id'.
     1674 *
    16701675 * @package WordPress
    16711676 * @subpackage Taxonomy
    16721677 * @since 2.3.0
     
    17461751                $select_this = 't.term_id';
    17471752        else if ( 'names' == $fields )
    17481753                $select_this = 't.name';
     1754        else if ( 'id=>name' == $fields )
     1755                $select_this = 't.term_id,t.name';
     1756        else if ( 'id=>term' == $fields )
     1757                $select_this = 't.*';
    17491758        else if ( 'all_with_object_id' == $fields )
    17501759                $select_this = 't.*, tt.*, tr.object_id';
    17511760
    1752         $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order";
     1761        $where_parent = (empty($parent) ? '' : " tt.parent={$parent} AND");
     1762        $distinct = ('all_with_object_id' == $fields ? '' : ' DISTINCT');
    17531763
    1754         if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
    1755                 $terms = array_merge($terms, $wpdb->get_results($query));
    1756                 update_term_cache($terms);
    1757         } else if ( 'ids' == $fields || 'names' == $fields ) {
    1758                 $terms = array_merge($terms, $wpdb->get_col($query));
    1759         } else if ( 'tt_ids' == $fields ) {
    1760                 $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order");
     1764        if ( 'tt_ids' == $fields ) {
     1765                $terms = $wpdb->get_col("SELECT{$distinct} tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE{$where_parent} tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order");
     1766        } else {
     1767                $query = "SELECT{$distinct} DISTINCT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE{$where_parent} tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order";
     1768                if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
     1769                        $terms = array_merge($terms, $wpdb->get_results($query));
     1770                        update_term_cache($terms);
     1771                } else if ( 'ids' == $fields || 'names' == $fields ) {
     1772                        $terms = array_merge($terms, $wpdb->get_col($query));
     1773                }
    17611774        }
    17621775
    17631776        if ( ! $terms )
    17641777                $terms = array();
    17651778
     1779        if ( 'id=>name' == $fields ) {
     1780                $_terms = array();
     1781                foreach($terms as $term)
     1782                        $_terms[$term->term_id] = $term->name;
     1783          $terms = $_terms;
     1784        } else if ( 'id=>term' == $fields ) {
     1785                $_terms = array();
     1786                foreach($terms as $term)
     1787                        $_terms[$term->term_id] = $term;
     1788          $terms = $_terms;
     1789        }
     1790
    17661791        return apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args);
    17671792}
    17681793