Skip to content

Commit b87bd75

Browse files
author
boonebgorges
committed
Allow term meta lazy-loading to be selectively disabled in WP_Query.
The process of lazy-loading can be resource intensive for object that have terms in large numbers of taxonomies and are running a persistent object cache. This new parameter allows the feature to be disabled in these cases. Props DBrumbaugh10Up. See #36953. git-svn-id: https://develop.svn.wordpress.org/trunk@37589 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 083d66b commit b87bd75

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/wp-includes/query.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ public function fill_query_vars($array) {
14681468
* @since 4.5.0 Removed the `$comments_popup` parameter.
14691469
* Introduced the `$comment_status` and `$ping_status` parameters.
14701470
* Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts.
1471-
* @since 4.6.0 Added 'post_name__in' support for `$orderby`.
1471+
* @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced `$lazy_load_term_meta`.
14721472
* @access public
14731473
*
14741474
* @param string|array $query {
@@ -1567,6 +1567,10 @@ public function fill_query_vars($array) {
15671567
* @type string $title Post title.
15681568
* @type bool $update_post_meta_cache Whether to update the post meta cache. Default true.
15691569
* @type bool $update_post_term_cache Whether to update the post term cache. Default true.
1570+
* @type bool $lazy_load_term_meta Whether to lazy-load term meta. Setting to false will
1571+
* disable cache priming for term meta, so that each
1572+
* get_term_meta() call will hit the database.
1573+
* Defaults to the value of `$update_post_term_cache`.
15701574
* @type int $w The week number of the year. Default empty. Accepts numbers 0-53.
15711575
* @type int $year The four-digit year. Default empty. Accepts any four-digit year.
15721576
* }
@@ -2545,6 +2549,10 @@ public function get_posts() {
25452549
if ( !isset($q['update_post_term_cache']) )
25462550
$q['update_post_term_cache'] = true;
25472551

2552+
if ( ! isset( $q['lazy_load_term_meta'] ) ) {
2553+
$q['lazy_load_term_meta'] = $q['update_post_term_cache'];
2554+
}
2555+
25482556
if ( !isset($q['update_post_meta_cache']) )
25492557
$q['update_post_meta_cache'] = true;
25502558

@@ -3782,7 +3790,7 @@ public function get_posts() {
37823790
$this->posts = array();
37833791
}
37843792

3785-
if ( $q['update_post_term_cache'] ) {
3793+
if ( $q['lazy_load_term_meta'] ) {
37863794
wp_queue_posts_for_term_meta_lazyload( $this->posts );
37873795
}
37883796

tests/phpunit/tests/term/meta.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,61 @@ public function test_term_meta_should_be_lazy_loaded_for_all_terms_in_wp_query_l
150150
}
151151
}
152152

153+
/**
154+
* @ticket 36593
155+
*/
156+
public function test_lazy_load_term_meta_should_fall_back_on_update_post_term_cache() {
157+
$q = new WP_Query( array(
158+
'update_post_term_cache' => true,
159+
) );
160+
161+
$this->assertTrue( $q->get( 'lazy_load_term_meta' ) );
162+
163+
$q = new WP_Query( array(
164+
'update_post_term_cache' => false,
165+
) );
166+
167+
$this->assertFalse( $q->get( 'lazy_load_term_meta' ) );
168+
}
169+
170+
/**
171+
* @ticket 36593
172+
*/
173+
public function test_lazy_load_term_meta_false() {
174+
global $wpdb;
175+
176+
$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
177+
178+
register_taxonomy( 'wptests_tax', 'post' );
179+
$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
180+
wp_set_object_terms( $p, $terms, 'wptests_tax' );
181+
foreach ( $terms as $t ) {
182+
add_term_meta( $t, 'foo', 'bar' );
183+
}
184+
185+
$q = new WP_Query( array(
186+
'cache_results' => true,
187+
'update_post_term_cache' => true,
188+
'lazy_load_term_meta' => false,
189+
) );
190+
191+
if ( $q->have_posts() ) {
192+
while ( $q->have_posts() ) {
193+
$q->the_post();
194+
195+
// Requests will hit the database.
196+
$num_queries = $wpdb->num_queries;
197+
$this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
198+
$num_queries++;
199+
$this->assertSame( $num_queries, $wpdb->num_queries );
200+
201+
$this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
202+
$num_queries++;
203+
$this->assertSame( $num_queries, $wpdb->num_queries );
204+
}
205+
}
206+
}
207+
153208
public function test_adding_term_meta_should_bust_get_terms_cache() {
154209
$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
155210

0 commit comments

Comments
 (0)