mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-03-31 10:44:26 +00:00
Improve lazyloading of term metadata in WP_Query loops.
[34529] introduced lazyloading for the metadata belonging to terms matching posts in the main `WP_Query`. The current changeset improves this technique in the following ways: * Term meta lazyloading is now performed on the results of all `WP_Query` queries, not just the main query. * Fewer global variable touches and greater encapsulation. * The logic for looping through posts to identify terms is now only performed once per `WP_Query`. Props dlh, boonebgorges. See #34047. git-svn-id: https://develop.svn.wordpress.org/trunk@34704 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -145,6 +145,58 @@ class Tests_Term_Meta extends WP_UnitTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 34073
|
||||
*/
|
||||
public function test_term_meta_should_be_lazy_loaded_only_for_the_queries_in_which_the_term_has_posts() {
|
||||
global $wpdb;
|
||||
|
||||
$posts = $this->factory->post->create_many( 3, array( 'post_status' => 'publish' ) );
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$terms = $this->factory->term->create_many( 6, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
|
||||
wp_set_object_terms( $posts[0], array( $terms[0], $terms[1] ), 'wptests_tax' );
|
||||
wp_set_object_terms( $posts[1], array( $terms[2], $terms[3] ), 'wptests_tax' );
|
||||
wp_set_object_terms( $posts[2], array( $terms[0], $terms[4], $terms[5] ), 'wptests_tax' );
|
||||
|
||||
foreach ( $terms as $t ) {
|
||||
add_term_meta( $t, 'foo', 'bar' );
|
||||
}
|
||||
|
||||
$q0 = new WP_Query( array( 'p' => $posts[0] ) );
|
||||
$q1 = new WP_Query( array( 'p' => $posts[1] ) );
|
||||
$q2 = new WP_Query( array( 'p' => $posts[2] ) );
|
||||
|
||||
/*
|
||||
* $terms[0] belongs to both $posts[0] and $posts[2], so `get_term_meta( $terms[0] )` should prime
|
||||
* the cache for term matched by $q0 and $q2.
|
||||
*/
|
||||
|
||||
// First request will hit the database.
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
// Prime caches.
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
|
||||
|
||||
// Two queries: one for $q0 and one for $q2.
|
||||
$num_queries += 2;
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// Next requests should be in cache.
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[4], 'foo', true ) );
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[5], 'foo', true ) );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// Querying for $terms[2] will prime $terms[3] as well.
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) );
|
||||
$num_queries++;
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[3], 'foo', true ) );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
public function test_adding_term_meta_should_bust_get_terms_cache() {
|
||||
$terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user