Avoid redundant table joins in WP_Tax_Query.

IN clauses that are connected by OR require only a single table join. To avoid
extraneous joins, keep track of generated table aliases, and let sibling
clauses piggy-back on those aliases when possible.

Introduces WP_Tax_Query::sanitize_relation() to reduce some repeated code.

Adds unit tests to verify the JOIN consolidation, and integration tests for
cases where JOINS are being combined.

Props boonebgorges, otto42, jakub.tyrcha.
Fixes #18105.

git-svn-id: https://develop.svn.wordpress.org/trunk@29902 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2014-10-15 16:39:19 +00:00
parent c995866623
commit 56c82e534f
3 changed files with 284 additions and 13 deletions
+46
View File
@@ -1374,6 +1374,52 @@ class Tests_Post_Query extends WP_UnitTestCase {
$this->assertEquals( array( $p3 ), $q->posts );
}
/**
* @group taxonomy
* @ticket 18105
*/
public function test_tax_query_single_query_multiple_queries_operator_not_in() {
$t1 = $this->factory->term->create( array(
'taxonomy' => 'category',
'slug' => 'foo',
'name' => 'Foo',
) );
$t2 = $this->factory->term->create( array(
'taxonomy' => 'category',
'slug' => 'bar',
'name' => 'Bar',
) );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
wp_set_post_terms( $p1, $t1, 'category' );
wp_set_post_terms( $p2, $t2, 'category' );
$q = new WP_Query( array(
'fields' => 'ids',
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'terms' => array( 'foo' ),
'field' => 'slug',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'category',
'terms' => array( 'bar' ),
'field' => 'slug',
'operator' => 'NOT IN',
),
),
) );
$this->assertEquals( array( $p3 ), $q->posts );
}
/**
* @group taxonomy
*/