Taxonomy: Cache results of term count queries.

Fixes #38295.

git-svn-id: https://develop.svn.wordpress.org/trunk@38784 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-10-12 15:29:03 +00:00
parent a1531d97af
commit 7e8e642ead
2 changed files with 62 additions and 1 deletions

View File

@ -690,7 +690,9 @@ class WP_Term_Query {
}
if ( 'count' == $_fields ) {
return $wpdb->get_var( $this->request );
$count = $wpdb->get_var( $this->request );
wp_cache_set( $cache_key, $count, 'terms' );
return $count;
}
$terms = $wpdb->get_results( $this->request );

View File

@ -322,4 +322,63 @@ class Tests_Term_Query extends WP_UnitTestCase {
$this->assertEqualSets( array( $terms[1] ), $found );
}
/**
* @ticket 38295
* @group cache
*/
public function test_count_query_should_be_cached() {
global $wpdb;
register_taxonomy( 'wptests_tax_1', 'post' );
$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax_1' ) );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
) );
$count = $query->get_terms();
$this->assertEquals( 2, $count );
$num_queries = $wpdb->num_queries;
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
) );
$count = $query->get_terms();
$this->assertEquals( 2, $count );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 38295
* @group cache
*/
public function test_count_query_cache_should_be_invalidated_with_incrementor_bump() {
register_taxonomy( 'wptests_tax_1', 'post' );
$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax_1' ) );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
) );
$count = $query->get_terms();
$this->assertEquals( 2, $count );
wp_delete_term( $terms[0], 'wptests_tax_1' );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
) );
$count = $query->get_terms();
$this->assertEquals( 1, $count );
}
}