From 0d0b17b61700ca77a7ea8ffa837d385f83068a5e Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 4 Sep 2013 18:16:31 +0000 Subject: [PATCH] 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 --- src/wp-includes/query.php | 20 ++++++++++----- tests/phpunit/tests/term/query.php | 40 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 tests/phpunit/tests/term/query.php diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 8d52b2e169..af558fe06e 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -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'], diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php new file mode 100644 index 0000000000..15c85a0dd4 --- /dev/null +++ b/tests/phpunit/tests/term/query.php @@ -0,0 +1,40 @@ +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 ); + } +} \ No newline at end of file