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/getTerms.php

    r33903 r34529  
    2323
    2424        // last_changed and num_queries should bump
    25         $terms = get_terms( 'post_tag' );
     25        $terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) );
    2626        $this->assertEquals( 3, count( $terms ) );
    2727        $time1 = wp_cache_get( 'last_changed', 'terms' );
     
    3232
    3333        // Again. last_changed and num_queries should remain the same.
    34         $terms = get_terms( 'post_tag' );
     34        $terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) );
    3535        $this->assertEquals( 3, count( $terms ) );
    3636        $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
     
    15031503    }
    15041504
     1505    /**
     1506     * @ticket 10142
     1507     */
     1508    public function test_termmeta_cache_should_be_primed_by_default() {
     1509        global $wpdb;
     1510
     1511        register_taxonomy( 'wptests_tax', 'post' );
     1512        $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     1513        add_term_meta( $terms[0], 'foo', 'bar' );
     1514        add_term_meta( $terms[1], 'foo', 'bar' );
     1515        add_term_meta( $terms[2], 'foo', 'bar' );
     1516
     1517        $found = get_terms( 'wptests_tax', array(
     1518            'hide_empty' => false,
     1519            'include' => $terms,
     1520        ) );
     1521
     1522        $num_queries = $wpdb->num_queries;
     1523
     1524        foreach ( $terms as $t ) {
     1525            $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
     1526        }
     1527
     1528        $this->assertSame( $num_queries, $wpdb->num_queries );
     1529    }
     1530
     1531    /**
     1532     * @ticket 10142
     1533     */
     1534    public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() {
     1535        global $wpdb;
     1536
     1537        register_taxonomy( 'wptests_tax', 'post' );
     1538        $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     1539        add_term_meta( $terms[0], 'foo', 'bar' );
     1540        add_term_meta( $terms[1], 'foo', 'bar' );
     1541        add_term_meta( $terms[2], 'foo', 'bar' );
     1542
     1543        $found = get_terms( 'wptests_tax', array(
     1544            'hide_empty' => false,
     1545            'include' => $terms,
     1546            'update_term_meta_cache' => false,
     1547        ) );
     1548
     1549        $num_queries = $wpdb->num_queries;
     1550
     1551        foreach ( $terms as $t ) {
     1552            $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
     1553        }
     1554
     1555        $this->assertSame( $num_queries + 3, $wpdb->num_queries );
     1556    }
     1557
     1558    /**
     1559     * @ticket 10142
     1560     */
     1561    public function test_meta_query() {
     1562        register_taxonomy( 'wptests_tax', 'post' );
     1563        $terms = $this->factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
     1564        add_term_meta( $terms[0], 'foo', 'bar' );
     1565        add_term_meta( $terms[1], 'foo', 'bar' );
     1566        add_term_meta( $terms[2], 'foo', 'baz' );
     1567        add_term_meta( $terms[3], 'foob', 'ar' );
     1568
     1569        $found = get_terms( 'wptests_tax', array(
     1570            'hide_empty' => false,
     1571            'meta_query' => array(
     1572                array(
     1573                    'key' => 'foo',
     1574                    'value' => 'bar',
     1575                ),
     1576            ),
     1577            'fields' => 'ids',
     1578        ) );
     1579
     1580        $this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
     1581    }
     1582
    15051583    protected function create_hierarchical_terms_and_posts() {
    15061584        $terms = array();
Note: See TracChangeset for help on using the changeset viewer.