Convert `category__and` to `category__in` (less expensive) and unset it when only one category is passed. Adds unit tests.
Fixes #24245. git-svn-id: https://develop.svn.wordpress.org/trunk@25238 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
14da301031
commit
0d0b17b617
|
@ -1762,8 +1762,16 @@ class WP_Query {
|
|||
$q['cat'] = implode(',', $req_cats);
|
||||
}
|
||||
|
||||
if ( !empty($q['category__in']) ) {
|
||||
$q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
|
||||
if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
|
||||
$q['category__and'] = (array) $q['category__and'];
|
||||
if ( ! isset( $q['category__in'] ) )
|
||||
$q['category__in'] = array();
|
||||
$q['category__in'][] = absint( reset( $q['category__and'] ) );
|
||||
unset( $q['category__and'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $q['category__in'] ) ) {
|
||||
$q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
|
||||
$tax_query[] = array(
|
||||
'taxonomy' => 'category',
|
||||
'terms' => $q['category__in'],
|
||||
|
@ -1772,8 +1780,8 @@ class WP_Query {
|
|||
);
|
||||
}
|
||||
|
||||
if ( !empty($q['category__not_in']) ) {
|
||||
$q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
|
||||
if ( ! empty($q['category__not_in']) ) {
|
||||
$q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
|
||||
$tax_query[] = array(
|
||||
'taxonomy' => 'category',
|
||||
'terms' => $q['category__not_in'],
|
||||
|
@ -1782,8 +1790,8 @@ class WP_Query {
|
|||
);
|
||||
}
|
||||
|
||||
if ( !empty($q['category__and']) ) {
|
||||
$q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
|
||||
if ( ! empty($q['category__and']) ) {
|
||||
$q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
|
||||
$tax_query[] = array(
|
||||
'taxonomy' => 'category',
|
||||
'terms' => $q['category__and'],
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @group taxonomy
|
||||
*/
|
||||
class Tests_Tax_Query extends WP_UnitTestCase {
|
||||
protected $q;
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
unset( $this->q );
|
||||
$this->q = new WP_Query();
|
||||
}
|
||||
|
||||
function test_category__and_var() {
|
||||
$term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
|
||||
$term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
|
||||
$post_id = $this->factory->post->create();
|
||||
|
||||
wp_set_post_categories( $post_id, $term_id );
|
||||
|
||||
$posts = $this->q->query( array( 'category__and' => array( $term_id ) ) );
|
||||
|
||||
$this->assertEmpty( $this->q->get( 'category__and' ) );
|
||||
$this->assertCount( 0, $this->q->get( 'category__and' ) );
|
||||
$this->assertNotEmpty( $this->q->get( 'category__in' ) );
|
||||
$this->assertCount( 1, $this->q->get( 'category__in' ) );
|
||||
|
||||
$this->assertNotEmpty( $posts );
|
||||
$this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
|
||||
|
||||
$posts2 = $this->q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
|
||||
$this->assertNotEmpty( $this->q->get( 'category__and' ) );
|
||||
$this->assertCount( 2, $this->q->get( 'category__and' ) );
|
||||
$this->assertEmpty( $this->q->get( 'category__in' ) );
|
||||
$this->assertCount( 0, $this->q->get( 'category__in' ) );
|
||||
|
||||
$this->assertEmpty( $posts2 );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue