WordPress.org

Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.