WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/25/2015 03:58:59 AM (5 years ago)
Author:
boonebgorges
Message:

Introduce metadata for taxonomy terms.

Adds a new table to the database schema (wp_termmeta), and a set of
*_term_meta() API functions. get_terms() and wp_get_object_terms()
now also support 'meta_query' parameters, with syntax identical to other
uses of WP_Meta_Query.

When fetching terms via get_terms() or wp_get_object_terms(), metadata for
matched terms is preloaded into the cache by default. Disable this behavior
by setting the new $update_term_meta_cache paramater to false.

To maximize performance, within WP_Query loops, the termmeta cache is *not*
primed by default. Instead, we use a lazy-loading technique: metadata for all
terms belonging to posts in the loop is loaded into the cache the first time
that get_term_meta() is called within the loop.

Props boonebgorges, sirzooro.
See #10142.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r31270 r34529  
    419419    }
    420420
     421    /**
     422     * @ticket 10142
     423     */
     424    public function test_termmeta_cache_should_be_primed_by_default() {
     425        global $wpdb;
     426
     427        register_taxonomy( 'wptests_tax', 'post' );
     428        $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     429        add_term_meta( $terms[0], 'foo', 'bar' );
     430        add_term_meta( $terms[1], 'foo', 'bar' );
     431        add_term_meta( $terms[2], 'foo', 'bar' );
     432
     433        $p = $this->factory->post->create();
     434        wp_set_object_terms( $p, $terms, 'wptests_tax' );
     435
     436        $found = wp_get_object_terms( $p, 'wptests_tax' );
     437
     438        $num_queries = $wpdb->num_queries;
     439
     440        foreach ( $terms as $t ) {
     441            $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
     442        }
     443
     444        $this->assertSame( $num_queries, $wpdb->num_queries );
     445    }
     446
     447    /**
     448     * @ticket 10142
     449     */
     450    public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() {
     451        global $wpdb;
     452
     453        register_taxonomy( 'wptests_tax', 'post' );
     454        $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     455        add_term_meta( $terms[0], 'foo', 'bar' );
     456        add_term_meta( $terms[1], 'foo', 'bar' );
     457        add_term_meta( $terms[2], 'foo', 'bar' );
     458
     459        $p = $this->factory->post->create();
     460        wp_set_object_terms( $p, $terms, 'wptests_tax' );
     461
     462        $found = wp_get_object_terms( $p, 'wptests_tax', array(
     463            'update_term_meta_cache' => false,
     464        ) );
     465
     466        $num_queries = $wpdb->num_queries;
     467
     468        foreach ( $terms as $t ) {
     469            $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
     470        }
     471
     472        $this->assertSame( $num_queries + 3, $wpdb->num_queries );
     473    }
     474
     475    /**
     476     * @ticket 10142
     477     */
     478    public function test_meta_query() {
     479        register_taxonomy( 'wptests_tax', 'post' );
     480        $terms = $this->factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
     481        add_term_meta( $terms[0], 'foo', 'bar' );
     482        add_term_meta( $terms[1], 'foo', 'bar' );
     483        add_term_meta( $terms[2], 'foo', 'baz' );
     484        add_term_meta( $terms[3], 'foob', 'ar' );
     485
     486        $p = $this->factory->post->create();
     487        wp_set_object_terms( $p, $terms, 'wptests_tax' );
     488
     489        $found = wp_get_object_terms( $p, 'wptests_tax', array(
     490            'meta_query' => array(
     491                array(
     492                    'key' => 'foo',
     493                    'value' => 'bar',
     494                ),
     495            ),
     496        ) );
     497
     498        $this->assertEqualSets( array( $terms[0], $terms[1] ), wp_list_pluck( $found, 'term_id' ) );
     499    }
     500
    421501    public function filter_get_object_terms( $terms ) {
    422502        $term_ids = wp_list_pluck( $terms, 'term_id' );
Note: See TracChangeset for help on using the changeset viewer.