WordPress.org

Make WordPress Core

Changeset 38667


Ignore:
Timestamp:
09/28/2016 03:54:36 AM (3 years ago)
Author:
boonebgorges
Message:

Taxonomy: Use WP_Term_Query when querying for object terms.

The new 'object_ids' parameter for WP_Term_Query allows queries for
terms that "belong to" a given object. This change makes it possible
to use WP_Term_Query inside of wp_get_object_terms(), rather than
assembling a SQL query.

The refactor has a couple of benefits:

  • Less redundancy.
  • Better consistency in accepted arguments between the term query functions. See #31105.
  • Less redundancy.
  • Object term queries are now cached. The get_object_term_cache() cache remains, and will be a somewhat less fragile secondary cache in front of the query cache (which is subject to frequent invalidation).
  • Less redundancy.

A small breaking change: Previously, if a non-hierarchical taxonomy had
terms that had a non-zero 'parent' (perhaps because of a direct SQL
query), wp_get_object_terms() would respect the 'parent' argument.
This is in contrast to WP_Term_Query and get_terms(), which have
always rejected 'parent' queries for non-hierarchical taxonomies. For
consistency, the behavior of get_terms() is being applied across the
board: passing 'parent' for a non-hierarchical taxonomy will result in
an empty result set (since the cached taxonomy hierarchy will be empty).

Props flixos90, boonebgorges.
See #37198.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r38500 r38667  
    101101     * @since 4.6.0
    102102     * @since 4.6.0 Introduced 'term_taxonomy_id' parameter.
     103     * @since 4.7.0 Introduced 'object_ids' parameter.
    103104     * @access public
    104105     *
     
    108109     *     @type string|array $taxonomy               Taxonomy name, or array of taxonomies, to which results should
    109110     *                                                be limited.
     111     *     @type int|array    $object_ids             Optional. Object ID, or array of object IDs. Results will be
     112     *                                                limited to terms associated with these objects.
    110113     *     @type string       $orderby                Field(s) to order terms by. Accepts term fields ('name',
    111114     *                                                'slug', 'term_group', 'term_id', 'id', 'description'),
     
    131134     *     @type int          $offset                 The number by which to offset the terms query. Default empty.
    132135     *     @type string       $fields                 Term fields to query for. Accepts 'all' (returns an array of
    133      *                                                complete term objects), 'ids' (returns an array of ids),
    134      *                                                'id=>parent' (returns an associative array with ids as keys,
    135      *                                                parent term IDs as values), 'names' (returns an array of term
    136      *                                                names), 'count' (returns the number of matching terms),
    137      *                                                'id=>name' (returns an associative array with ids as keys,
    138      *                                                term names as values), or 'id=>slug' (returns an associative
    139      *                                                array with ids as keys, term slugs as values). Default 'all'.
     136     *                                                complete term objects), 'all_with_object_id' (returns an
     137     *                                                array of term objects with the 'object_id' param; only works
     138     *                                                when the `$fields` parameter is 'object_ids' ), 'ids'
     139     *                                                (returns an array of ids), 'tt_ids' (returns an array of
     140     *                                                term taxonomy ids), 'id=>parent' (returns an associative
     141     *                                                array with ids as keys, parent term IDs as values), 'names'
     142     *                                                (returns an array of term names), 'count' (returns the number
     143     *                                                of matching terms), 'id=>name' (returns an associative array
     144     *                                                with ids as keys, term names as values), or 'id=>slug'
     145     *                                                (returns an associative array with ids as keys, term slugs
     146     *                                                as values). Default 'all'.
    140147     *     @type bool         $count                  Whether to return a term count (true) or array of term objects
    141148     *                                                (false). Will take precedence over `$fields` if true.
     
    184191        $this->query_var_defaults = array(
    185192            'taxonomy'               => null,
     193            'object_ids'             => null,
    186194            'orderby'                => 'name',
    187195            'order'                  => 'ASC',
     
    393401        }
    394402
    395         $orderby = $this->parse_orderby( $this->query_vars['orderby'] );
     403        // 'term_order' is a legal sort order only when joining the relationship table.
     404        $_orderby = $this->query_vars['orderby'];
     405        if ( 'term_order' === $_orderby && empty( $this->query_vars['object_ids'] ) ) {
     406            $_orderby = 'term_id';
     407        }
     408        $orderby = $this->parse_orderby( $_orderby );
     409
    396410        if ( $orderby ) {
    397411            $orderby = "ORDER BY $orderby";
     
    508522        }
    509523
     524        if ( ! empty( $args['object_ids'] ) ) {
     525            $object_ids = $args['object_ids'];
     526            if ( ! is_array( $object_ids ) ) {
     527                $object_ids = array( $object_ids );
     528            }
     529
     530            $object_ids = implode( ', ', array_map( 'intval', $object_ids ) );
     531            $this->sql_clauses['where']['object_ids'] = "tr.object_id IN ($object_ids)";
     532        }
     533
     534        /*
     535         * When querying for object relationships, the 'count > 0' check
     536         * added by 'hide_empty' is superfluous.
     537         */
     538        if ( ! empty( $args['object_ids'] ) ) {
     539            $args['hide_empty'] = false;
     540        }
     541
    510542        if ( '' !== $parent ) {
    511543            $parent = (int) $parent;
     
    559591        switch ( $args['fields'] ) {
    560592            case 'all':
     593            case 'all_with_object_id' :
     594            case 'tt_ids' :
     595            case 'slugs' :
    561596                $selects = array( 't.*', 'tt.*' );
     597                if ( 'all_with_object_id' === $args['fields'] && ! empty( $args['object_ids'] ) ) {
     598                    $selects[] = 'tr.object_id';
     599                }
    562600                break;
    563601            case 'ids':
     
    603641        $join .= " INNER JOIN {$this->db->term_taxonomy} AS tt ON t.term_id = tt.term_id";
    604642
     643        if ( ! empty( $this->query_vars['object_ids'] ) ) {
     644            $join .= " INNER JOIN {$this->db->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id";
     645        }
     646
    605647        $where = implode( ' AND ', $this->sql_clauses['where'] );
    606648
     
    658700
    659701        $terms = $this->db->get_results( $this->request );
    660         if ( 'all' == $_fields ) {
     702        if ( 'all' == $_fields || 'all_with_object_id' === $_fields ) {
    661703            update_term_cache( $terms );
    662704        }
     
    709751        }
    710752
     753        /*
     754         * When querying for terms connected to objects, we may get
     755         * duplicate results. The duplicates should be preserved if
     756         * `$fields` is 'all_with_object_id', but should otherwise be
     757         * removed.
     758         */
     759        if ( ! empty( $args['object_ids'] ) && 'all_with_object_id' != $_fields ) {
     760            $_tt_ids = $_terms = array();
     761            foreach ( $terms as $term ) {
     762                if ( isset( $_tt_ids[ $term->term_id ] ) ) {
     763                    continue;
     764                }
     765
     766                $_tt_ids[ $term->term_id ] = 1;
     767                $_terms[] = $term;
     768            }
     769
     770            $terms = $_terms;
     771        }
     772
    711773        $_terms = array();
    712774        if ( 'id=>parent' == $_fields ) {
     
    716778        } elseif ( 'ids' == $_fields ) {
    717779            foreach ( $terms as $term ) {
    718                 $_terms[] = $term->term_id;
     780                $_terms[] = (int) $term->term_id;
     781            }
     782        } elseif ( 'tt_ids' == $_fields ) {
     783            foreach ( $terms as $term ) {
     784                $_terms[] = (int) $term->term_taxonomy_id;
    719785            }
    720786        } elseif ( 'names' == $_fields ) {
    721787            foreach ( $terms as $term ) {
    722788                $_terms[] = $term->name;
     789            }
     790        } elseif ( 'slugs' == $_fields ) {
     791            foreach ( $terms as $term ) {
     792                $_terms[] = $term->slug;
    723793            }
    724794        } elseif ( 'id=>name' == $_fields ) {
     
    747817        wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
    748818
    749         if ( 'all' === $_fields ) {
     819        if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) {
    750820            $terms = array_map( 'get_term', $terms );
    751821        }
     
    767837        $_orderby = strtolower( $orderby_raw );
    768838        $maybe_orderby_meta = false;
    769         if ( 'count' == $_orderby ) {
    770             $orderby = 'tt.count';
    771         } elseif ( 'name' == $_orderby ) {
    772             $orderby = 't.name';
    773         } elseif ( 'slug' == $_orderby ) {
    774             $orderby = 't.slug';
     839
     840        if ( in_array( $_orderby, array( 'term_id', 'name', 'slug', 'term_group' ), true ) ) {
     841            $orderby = "t.$_orderby";
     842        } elseif ( in_array( $_orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id', 'description' ), true ) ) {
     843            $orderby = "tt.$_orderby";
     844        } elseif ( 'term_order' === $_orderby ) {
     845            $orderby = 'tr.term_order';
    775846        } elseif ( 'include' == $_orderby && ! empty( $this->query_vars['include'] ) ) {
    776847            $include = implode( ',', wp_parse_id_list( $this->query_vars['include'] ) );
    777848            $orderby = "FIELD( t.term_id, $include )";
    778         } elseif ( 'term_group' == $_orderby ) {
    779             $orderby = 't.term_group';
    780         } elseif ( 'description' == $_orderby ) {
    781             $orderby = 'tt.description';
    782849        } elseif ( 'none' == $_orderby ) {
    783850            $orderby = '';
  • trunk/src/wp-includes/taxonomy.php

    r38621 r38667  
    19421942 * @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments. When `$fields` is 'all' or
    19431943 *              'all_with_object_id', an array of `WP_Term` objects will be returned.
     1944 * @since 4.7.0 Refactored to use WP_Term_Query, and to support any WP_Term_Query arguments.
    19441945 *
    19451946 * @global wpdb $wpdb WordPress database abstraction object.
     
    19471948 * @param int|array    $object_ids The ID(s) of the object(s) to retrieve.
    19481949 * @param string|array $taxonomies The taxonomies to retrieve terms from.
    1949  * @param array|string $args {
    1950  *     Array of arguments.
    1951  *     @type string $orderby                Field by which results should be sorted. Accepts 'name', 'count', 'slug',
    1952  *                                          'term_group', 'term_order', 'taxonomy', 'parent', or 'term_taxonomy_id'.
    1953  *                                          Default 'name'.
    1954  *     @type string $order                  Sort order. Accepts 'ASC' or 'DESC'. Default 'ASC'.
    1955  *     @type string $fields                 Fields to return for matched terms. Accepts 'all', 'ids', 'names', and
    1956  *                                          'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result
    1957  *                                          in an array of term objects being returned, 'ids' will return an array of
    1958  *                                          integers, and 'names' an array of strings.
    1959  *     @type int    $parent                 Optional. Limit results to the direct children of a given term ID.
    1960  *     @type bool   $update_term_meta_cache Whether to prime termmeta cache for matched terms. Only applies when
    1961  *                                          `$fields` is 'all', 'all_with_object_id', or 'term_id'. Default true.
    1962  *     @type array  $meta_query             Meta query clauses to limit retrieved terms by. See `WP_Meta_Query`.
    1963  *                                          Default empty.
    1964  * }
     1950 * @param array|string $args       See WP_Term_Query::__construct() for supported arguments.
    19651951 * @return array|WP_Error The requested term data or empty array if no terms found.
    19661952 *                        WP_Error if any of the $taxonomies don't exist.
     
    19841970    $object_ids = array_map('intval', $object_ids);
    19851971
    1986     $defaults = array(
    1987         'orderby' => 'name',
    1988         'order'   => 'ASC',
    1989         'fields'  => 'all',
    1990         'parent'  => '',
    1991         'update_term_meta_cache' => true,
    1992         'meta_query' => '',
    1993     );
    1994     $args = wp_parse_args( $args, $defaults );
    1995 
    1996     $terms = array();
    1997     if ( count($taxonomies) > 1 ) {
    1998         foreach ( $taxonomies as $index => $taxonomy ) {
    1999             $t = get_taxonomy($taxonomy);
    2000             if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {
    2001                 unset($taxonomies[$index]);
    2002                 $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
    2003             }
    2004         }
    2005     } else {
    2006         $t = get_taxonomy($taxonomies[0]);
    2007         if ( isset($t->args) && is_array($t->args) )
    2008             $args = array_merge($args, $t->args);
    2009     }
    2010 
    2011     $orderby = $args['orderby'];
    2012     $order = $args['order'];
    2013     $fields = $args['fields'];
    2014 
    2015     if ( in_array( $orderby, array( 'term_id', 'name', 'slug', 'term_group' ) ) ) {
    2016         $orderby = "t.$orderby";
    2017     } elseif ( in_array( $orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id' ) ) ) {
    2018         $orderby = "tt.$orderby";
    2019     } elseif ( 'term_order' === $orderby ) {
    2020         $orderby = 'tr.term_order';
    2021     } elseif ( 'none' === $orderby ) {
    2022         $orderby = '';
    2023         $order = '';
    2024     } else {
    2025         $orderby = 't.term_id';
    2026     }
    2027 
    2028     // tt_ids queries can only be none or tr.term_taxonomy_id
    2029     if ( ('tt_ids' == $fields) && !empty($orderby) )
    2030         $orderby = 'tr.term_taxonomy_id';
    2031 
    2032     if ( !empty($orderby) )
    2033         $orderby = "ORDER BY $orderby";
    2034 
    2035     $order = strtoupper( $order );
    2036     if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) )
    2037         $order = 'ASC';
    2038 
    2039     $taxonomy_array = $taxonomies;
    2040     $object_id_array = $object_ids;
    2041     $taxonomies = "'" . implode("', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
    2042     $object_ids = implode(', ', $object_ids);
    2043 
    2044     $select_this = '';
    2045     if ( 'all' == $fields ) {
    2046         $select_this = 't.*, tt.*';
    2047     } elseif ( 'ids' == $fields ) {
    2048         $select_this = 't.term_id';
    2049     } elseif ( 'names' == $fields ) {
    2050         $select_this = 't.name';
    2051     } elseif ( 'slugs' == $fields ) {
    2052         $select_this = 't.slug';
    2053     } elseif ( 'all_with_object_id' == $fields ) {
    2054         $select_this = 't.*, tt.*, tr.object_id';
    2055     }
    2056 
    2057     $where = array(
    2058         "tt.taxonomy IN ($taxonomies)",
    2059         "tr.object_id IN ($object_ids)",
    2060     );
    2061 
    2062     if ( '' !== $args['parent'] ) {
    2063         $where[] = $wpdb->prepare( 'tt.parent = %d', $args['parent'] );
    2064     }
    2065 
    2066     // Meta query support.
    2067     $meta_query_join = '';
    2068     if ( ! empty( $args['meta_query'] ) ) {
    2069         $mquery = new WP_Meta_Query( $args['meta_query'] );
    2070         $mq_sql = $mquery->get_sql( 'term', 't', 'term_id' );
    2071 
    2072         $meta_query_join .= $mq_sql['join'];
    2073 
    2074         // Strip leading AND.
    2075         $where[] = preg_replace( '/^\s*AND/', '', $mq_sql['where'] );
    2076     }
    2077 
    2078     $where = implode( ' AND ', $where );
    2079 
    2080     $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 $meta_query_join WHERE $where $orderby $order";
    2081 
    2082     $objects = false;
    2083     if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
    2084         $_terms = $wpdb->get_results( $query );
    2085         $object_id_index = array();
    2086         foreach ( $_terms as $key => $term ) {
    2087             $term = sanitize_term( $term, $taxonomy, 'raw' );
    2088             $_terms[ $key ] = $term;
    2089 
    2090             if ( isset( $term->object_id ) ) {
    2091                 $object_id_index[ $key ] = $term->object_id;
    2092             }
    2093         }
    2094 
    2095         update_term_cache( $_terms );
    2096         $_terms = array_map( 'get_term', $_terms );
    2097 
    2098         // Re-add the object_id data, which is lost when fetching terms from cache.
    2099         if ( 'all_with_object_id' === $fields ) {
    2100             foreach ( $_terms as $key => $_term ) {
    2101                 if ( isset( $object_id_index[ $key ] ) ) {
    2102                     $_term->object_id = $object_id_index[ $key ];
    2103                 }
    2104             }
    2105         }
    2106 
    2107         $terms = array_merge( $terms, $_terms );
    2108         $objects = true;
    2109 
    2110     } elseif ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) {
    2111         $_terms = $wpdb->get_col( $query );
    2112         $_field = ( 'ids' == $fields ) ? 'term_id' : 'name';
    2113         foreach ( $_terms as $key => $term ) {
    2114             $_terms[$key] = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' );
    2115         }
    2116         $terms = array_merge( $terms, $_terms );
    2117     } elseif ( 'tt_ids' == $fields ) {
    2118         $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");
    2119         foreach ( $terms as $key => $tt_id ) {
    2120             $terms[$key] = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context.
    2121         }
    2122     }
    2123 
    2124     // Update termmeta cache, if necessary.
    2125     if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_id' === $fields || 'ids' === $fields ) ) {
    2126         if ( 'ids' === $fields ) {
    2127             $term_ids = $terms;
    2128         } else {
    2129             $term_ids = wp_list_pluck( $terms, 'term_id' );
    2130         }
    2131 
    2132         update_termmeta_cache( $term_ids );
    2133     }
    2134 
    2135     if ( ! $terms ) {
    2136         $terms = array();
    2137     } elseif ( $objects && 'all_with_object_id' !== $fields ) {
    2138         $_tt_ids = array();
    2139         $_terms = array();
    2140         foreach ( $terms as $term ) {
    2141             if ( in_array( $term->term_taxonomy_id, $_tt_ids ) ) {
    2142                 continue;
    2143             }
    2144 
    2145             $_tt_ids[] = $term->term_taxonomy_id;
    2146             $_terms[] = $term;
    2147         }
    2148         $terms = $_terms;
    2149     } elseif ( ! $objects ) {
    2150         $terms = array_values( array_unique( $terms ) );
    2151     }
     1972    $args['taxonomy'] = $taxonomies;
     1973    $args['object_ids'] = $object_ids;
     1974
     1975    $terms = get_terms( $args );
    21521976
    21531977    /**
     
    21561980     * @since 4.2.0
    21571981     *
    2158      * @param array $terms           An array of terms for the given object or objects.
    2159      * @param array $object_id_array Array of object IDs for which `$terms` were retrieved.
    2160      * @param array $taxonomy_array Array of taxonomies from which `$terms` were retrieved.
    2161      * @param array $args            An array of arguments for retrieving terms for the given
    2162      *                               object(s). See wp_get_object_terms() for details.
     1982     * @param array $terms      An array of terms for the given object or objects.
     1983     * @param array $object_ids Array of object IDs for which `$terms` were retrieved.
     1984     * @param array $taxonomies Array of taxonomies from which `$terms` were retrieved.
     1985     * @param array $args       An array of arguments for retrieving terms for the given
     1986     *                          object(s). See wp_get_object_terms() for details.
    21631987     */
    2164     $terms = apply_filters( 'get_object_terms', $terms, $object_id_array, $taxonomy_array, $args );
     1988    $terms = apply_filters( 'get_object_terms', $terms, $object_ids, $taxonomies, $args );
     1989
     1990    $object_ids = implode( ',', $object_ids );
     1991    $taxonomies = implode( ',', $taxonomies );
    21651992
    21661993    /**
  • trunk/tests/phpunit/tests/term/query.php

    r38500 r38667  
    145145
    146146    /**
     147     * @ticket 37198
     148     */
     149    public function test_order_by_term_order_should_fall_back_on_term_id_when_relationship_table_is_not_being_joined() {
     150        register_taxonomy( 'wptests_tax', 'post' );
     151        $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     152        sort( $terms );
     153
     154        $q = new WP_Term_Query( array(
     155            'taxonomy' => 'wptests_tax',
     156            'orderby' => 'term_order',
     157            'fields' => 'ids',
     158            'hide_empty' => false,
     159        ) );
     160
     161        $this->assertSame( $terms, $q->get_terms() );
     162    }
     163
     164    /**
    147165     * @ticket 37591
    148166     */
     
    186204        $this->assertEquals( array( $t1, $t2 ), $terms );
    187205    }
     206
     207    /**
     208     * @ticket 37198
     209     */
     210    public function test_object_ids_single() {
     211        register_taxonomy( 'wptests_tax_1', 'post' );
     212
     213        $p = self::factory()->post->create();
     214        $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
     215
     216        wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
     217
     218        $query = new WP_Term_Query( array(
     219            'taxonomy' => 'wptests_tax_1',
     220            'object_ids' => $p,
     221            'fields' => 'ids',
     222        ) );
     223
     224        $this->assertEqualSets( array( $t ), $query->terms );
     225    }
     226
     227    /**
     228     * @ticket 37198
     229     */
     230    public function test_object_ids_array() {
     231        register_taxonomy( 'wptests_tax_1', 'post' );
     232
     233        $p = self::factory()->post->create();
     234        $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
     235
     236        wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
     237
     238        $query = new WP_Term_Query( array(
     239            'taxonomy' => 'wptests_tax_1',
     240            'object_ids' => array( $p ),
     241            'fields' => 'ids',
     242        ) );
     243
     244        $this->assertEqualSets( array( $t ), $query->terms );
     245    }
     246
     247    /**
     248     * @ticket 37198
     249     */
     250    public function test_duplicates_should_be_removed_for_fields_all() {
     251        register_taxonomy( 'wptests_tax_1', 'post' );
     252        $posts = self::factory()->post->create_many( 2 );
     253        $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
     254
     255        foreach ( $posts as $p ) {
     256            wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
     257        }
     258
     259        $query = new WP_Term_Query( array(
     260            'taxonomy' => 'wptests_tax_1',
     261            'object_ids' => $posts,
     262            'fields' => 'all',
     263        ) );
     264
     265        $this->assertSame( 1, count( $query->terms ) );
     266        $this->assertSame( $t, reset( $query->terms )->term_id );
     267    }
     268
     269    /**
     270     * @ticket 37198
     271     */
     272    public function test_duplicates_should_not_be_removed_for_fields_all_with_object_id() {
     273        register_taxonomy( 'wptests_tax_1', 'post' );
     274        $posts = self::factory()->post->create_many( 2 );
     275        $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
     276
     277        foreach ( $posts as $p ) {
     278            wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
     279        }
     280
     281        $query = new WP_Term_Query( array(
     282            'taxonomy' => 'wptests_tax_1',
     283            'object_ids' => $posts,
     284            'fields' => 'all_with_object_id',
     285        ) );
     286
     287        $this->assertSame( 2, count( $query->terms ) );
     288        foreach ( $query->terms as $term ) {
     289            $this->assertSame( $t, $term->term_id );
     290        }
     291    }
     292
     293    /**
     294     * @ticket 37198
     295     * @group cache
     296     */
     297    public function test_object_ids_cache_should_be_invalidated_by_term_relationship_change() {
     298        register_taxonomy( 'wptests_tax_1', 'post' );
     299
     300        $p = self::factory()->post->create();
     301        $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax_1' ) );
     302
     303        wp_set_object_terms( $p, array( $terms[0] ), 'wptests_tax_1' );
     304
     305        $query = new WP_Term_Query( array(
     306            'taxonomy' => 'wptests_tax_1',
     307            'object_ids' => $p,
     308            'fields' => 'ids',
     309        ) );
     310        $found = $query->get_terms();
     311
     312        $this->assertEqualSets( array( $terms[0] ), $found );
     313
     314        wp_set_object_terms( $p, array( $terms[1] ), 'wptests_tax_1' );
     315
     316        $query = new WP_Term_Query( array(
     317            'taxonomy' => 'wptests_tax_1',
     318            'object_ids' => $p,
     319            'fields' => 'ids',
     320        ) );
     321        $found = $query->get_terms();
     322
     323        $this->assertEqualSets( array( $terms[1] ), $found );
     324    }
    188325}
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r37567 r38667  
    2424
    2525        // make sure they're correct
    26         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'slugs', 'orderby' => 't.term_id'));
     26        $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'slugs', 'orderby' => 'term_id'));
    2727        $this->assertEquals( $terms_1_slugs, $terms );
    2828    }
     
    361361     */
    362362    public function test_parent() {
    363         $t1 = self::factory()->term->create( array(
    364             'taxonomy' => $this->taxonomy,
    365         ) );
    366         $t2 = self::factory()->term->create( array(
    367             'taxonomy' => $this->taxonomy,
    368         ) );
    369         $t3 = self::factory()->term->create( array(
    370             'taxonomy' => $this->taxonomy,
     363        register_taxonomy( 'wptests_tax2', 'post', array(
     364            'hierarchical' => true,
     365        ) );
     366
     367        $t1 = self::factory()->term->create( array(
     368            'taxonomy' => 'wptests_tax2',
     369        ) );
     370        $t2 = self::factory()->term->create( array(
     371            'taxonomy' => 'wptests_tax2',
     372        ) );
     373        $t3 = self::factory()->term->create( array(
     374            'taxonomy' => 'wptests_tax2',
    371375            'parent' => $t1,
    372376        ) );
    373377        $t4 = self::factory()->term->create( array(
    374             'taxonomy' => $this->taxonomy,
     378            'taxonomy' => 'wptests_tax2',
    375379            'parent' => $t2,
    376380        ) );
     
    378382        $p = self::factory()->post->create();
    379383
    380         wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
    381 
    382         $found = wp_get_object_terms( $p, $this->taxonomy, array(
     384        wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), 'wptests_tax2' );
     385
     386        $found = wp_get_object_terms( $p, 'wptests_tax2', array(
    383387            'parent' => $t1,
    384388            'fields' => 'ids',
  • trunk/tests/phpunit/tests/term/wpSetObjectTerms.php

    r38398 r38667  
    260260
    261261        // make sure they're correct
    262         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
     262        $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 'term_id'));
    263263        $this->assertEquals( $terms_1, $terms );
    264264
     
    268268
    269269        // make sure they're correct
    270         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
     270        $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 'term_id'));
    271271        $this->assertEquals( $terms_2, $terms );
    272272
     
    289289
    290290        // make sure they're correct
    291         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
     291        $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 'term_id'));
    292292        $this->assertEquals( $terms_1, $terms );
    293293
     
    297297
    298298        // make sure they're correct
    299         $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
     299        $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 'term_id'));
    300300        $this->assertEquals( $terms_2, $terms );
    301301
Note: See TracChangeset for help on using the changeset viewer.