mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-10-02 21:56:56 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@34529 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -418,6 +418,86 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
|
||||
$this->assertEqualSets( array( $t1, $t2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 10142
|
||||
*/
|
||||
public function test_termmeta_cache_should_be_primed_by_default() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
add_term_meta( $terms[0], 'foo', 'bar' );
|
||||
add_term_meta( $terms[1], 'foo', 'bar' );
|
||||
add_term_meta( $terms[2], 'foo', 'bar' );
|
||||
|
||||
$p = $this->factory->post->create();
|
||||
wp_set_object_terms( $p, $terms, 'wptests_tax' );
|
||||
|
||||
$found = wp_get_object_terms( $p, 'wptests_tax' );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
foreach ( $terms as $t ) {
|
||||
$this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
|
||||
}
|
||||
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 10142
|
||||
*/
|
||||
public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
add_term_meta( $terms[0], 'foo', 'bar' );
|
||||
add_term_meta( $terms[1], 'foo', 'bar' );
|
||||
add_term_meta( $terms[2], 'foo', 'bar' );
|
||||
|
||||
$p = $this->factory->post->create();
|
||||
wp_set_object_terms( $p, $terms, 'wptests_tax' );
|
||||
|
||||
$found = wp_get_object_terms( $p, 'wptests_tax', array(
|
||||
'update_term_meta_cache' => false,
|
||||
) );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
foreach ( $terms as $t ) {
|
||||
$this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
|
||||
}
|
||||
|
||||
$this->assertSame( $num_queries + 3, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 10142
|
||||
*/
|
||||
public function test_meta_query() {
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$terms = $this->factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
add_term_meta( $terms[0], 'foo', 'bar' );
|
||||
add_term_meta( $terms[1], 'foo', 'bar' );
|
||||
add_term_meta( $terms[2], 'foo', 'baz' );
|
||||
add_term_meta( $terms[3], 'foob', 'ar' );
|
||||
|
||||
$p = $this->factory->post->create();
|
||||
wp_set_object_terms( $p, $terms, 'wptests_tax' );
|
||||
|
||||
$found = wp_get_object_terms( $p, 'wptests_tax', array(
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'bar',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( $terms[0], $terms[1] ), wp_list_pluck( $found, 'term_id' ) );
|
||||
}
|
||||
|
||||
public function filter_get_object_terms( $terms ) {
|
||||
$term_ids = wp_list_pluck( $terms, 'term_id' );
|
||||
// all terms should still be objects
|
||||
|
||||
Reference in New Issue
Block a user