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/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',
Note: See TracChangeset for help on using the changeset viewer.