Skip to content

Commit 09c075a

Browse files
authored
Merge pull request #4268 from 10up/fix/4206
Fix: Ensure cache_results is set to true for Query
2 parents 5d8634a + 4f4f6f8 commit 09c075a

2 files changed

Lines changed: 141 additions & 14 deletions

File tree

includes/classes/Indexable/Post/QueryIntegration.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,6 @@ public function add_es_header( $query ) {
116116
return;
117117
}
118118

119-
/**
120-
* `cache_results` defaults to false but can be enabled.
121-
*
122-
* @since 1.5
123-
*/
124-
$query->set( 'cache_results', false );
125-
if ( ! empty( $query->query['cache_results'] ) ) {
126-
$query->set( 'cache_results', true );
127-
}
128-
129119
if ( ! headers_sent() ) {
130120
/**
131121
* Manually setting a header as $wp_query isn't yet initialized when we

tests/php/indexables/TestPost.php

Lines changed: 141 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3701,12 +3701,12 @@ public function testNoAvailablePostTypesToSearch() {
37013701
}
37023702

37033703
/**
3704-
* Test cache_results is off by default
3704+
* Test cache_results is on by default
37053705
*
37063706
* @since 1.5
37073707
* @group post
37083708
*/
3709-
public function testCacheResultsDefaultOff() {
3709+
public function testCacheResultsDefaultOn() {
37103710
$this->ep_factory->post->create();
37113711

37123712
ElasticPress\Elasticsearch::factory()->refresh_indices();
@@ -3718,7 +3718,7 @@ public function testCacheResultsDefaultOff() {
37183718
$query = new \WP_Query( $args );
37193719

37203720
$this->assertTrue( $query->elasticsearch_success );
3721-
$this->assertFalse( $query->query_vars['cache_results'] );
3721+
$this->assertTrue( $query->query_vars['cache_results'] );
37223722
}
37233723

37243724
/**
@@ -3784,7 +3784,8 @@ public function testCachedResultIsNotInCache() {
37843784
wp_cache_flush();
37853785

37863786
$args = array(
3787-
'ep_integrate' => true,
3787+
'ep_integrate' => true,
3788+
'cache_results' => false,
37883789
);
37893790

37903791
$query = new \WP_Query( $args );
@@ -10386,4 +10387,140 @@ public function throw_exception( $args ) {
1038610387
}
1038710388
return $args;
1038810389
}
10390+
10391+
/**
10392+
* Test that post meta and term caches are primed after ES query.
10393+
*
10394+
* @since 5.3.3
10395+
* @group post
10396+
*/
10397+
public function test_postmeta_and_term_caches_are_primed_after_ESQuery() {
10398+
global $wpdb;
10399+
10400+
$post_ids = $this->ep_factory->post->create_many(
10401+
2,
10402+
[
10403+
'meta_input' => [
10404+
'test_meta_key' => 'test_value',
10405+
],
10406+
'tax_input' => [
10407+
'category' => [ $this->ep_factory->category->create() ],
10408+
],
10409+
]
10410+
);
10411+
10412+
ElasticPress\Elasticsearch::factory()->refresh_indices();
10413+
10414+
wp_cache_flush();
10415+
10416+
$query = new \WP_Query(
10417+
[
10418+
'ep_integrate' => true,
10419+
'post__in' => $post_ids,
10420+
]
10421+
);
10422+
$this->assertTrue( $query->elasticsearch_success );
10423+
$this->assertCount( 2, $query->posts );
10424+
10425+
// After the query, post meta should be cached and no additional queries should be made.
10426+
$queries_before = $wpdb->num_queries;
10427+
10428+
foreach ( $post_ids as $post_id ) {
10429+
get_post_meta( $post_id, 'test_meta_key', true );
10430+
}
10431+
10432+
$queries_after = $wpdb->num_queries;
10433+
$this->assertSame( $queries_before, $queries_after );
10434+
10435+
foreach ( $post_ids as $post_id ) {
10436+
get_the_terms( $post_id, 'category' );
10437+
}
10438+
10439+
$queries_after = $wpdb->num_queries;
10440+
$this->assertSame( $queries_before, $queries_after );
10441+
}
10442+
10443+
/**
10444+
* Test that update_post_meta_cache query arg respects post meta cache.
10445+
*
10446+
* @since 5.3.3
10447+
* @group post
10448+
*/
10449+
public function test_update_post_meta_cache_query_arg_respects_post_meta_cache() {
10450+
global $wpdb;
10451+
10452+
$post_ids = $this->ep_factory->post->create_many(
10453+
2,
10454+
[
10455+
'meta_input' => [
10456+
'test_meta_key' => 'test_value',
10457+
],
10458+
]
10459+
);
10460+
10461+
ElasticPress\Elasticsearch::factory()->refresh_indices();
10462+
10463+
wp_cache_flush();
10464+
10465+
$query = new \WP_Query(
10466+
[
10467+
'ep_integrate' => true,
10468+
'post__in' => $post_ids,
10469+
'update_post_meta_cache' => false,
10470+
]
10471+
);
10472+
10473+
$this->assertTrue( $query->elasticsearch_success );
10474+
$this->assertCount( 2, $query->posts );
10475+
10476+
$queries_before = $wpdb->num_queries;
10477+
foreach ( $post_ids as $post_id ) {
10478+
get_post_meta( $post_id, 'test_meta_key', true );
10479+
}
10480+
10481+
$queries_after = $wpdb->num_queries;
10482+
$this->assertGreaterThan( $queries_before, $queries_after );
10483+
}
10484+
10485+
/**
10486+
* Test that update_post_term_cache query arg respects term cache.
10487+
*
10488+
* @since 5.3.3
10489+
* @group post
10490+
*/
10491+
public function test_update_post_term_cache_query_arg_respects_term_cache() {
10492+
global $wpdb;
10493+
$post_ids = $this->ep_factory->post->create_many(
10494+
2,
10495+
[
10496+
'tax_input' => [
10497+
'category' => [ $this->ep_factory->category->create() ],
10498+
],
10499+
]
10500+
);
10501+
10502+
ElasticPress\Elasticsearch::factory()->refresh_indices();
10503+
10504+
wp_cache_flush();
10505+
10506+
$query = new \WP_Query(
10507+
[
10508+
'ep_integrate' => true,
10509+
'post__in' => $post_ids,
10510+
'update_post_term_cache' => false,
10511+
]
10512+
);
10513+
10514+
$this->assertTrue( $query->elasticsearch_success );
10515+
$this->assertCount( 2, $query->posts );
10516+
10517+
$queries_before = $wpdb->num_queries;
10518+
10519+
foreach ( $post_ids as $post_id ) {
10520+
get_the_terms( $post_id, 'category' );
10521+
}
10522+
10523+
$queries_after = $wpdb->num_queries;
10524+
$this->assertGreaterThan( $queries_before, $queries_after );
10525+
}
1038910526
}

0 commit comments

Comments
 (0)