Ticket #24245: 24245.2.diff
File 24245.2.diff, 3.3 KB (added by , 12 years ago) |
---|
-
tests/phpunit/tests/term/query.php
1 <?php 2 3 /** 4 * @group taxonomy 5 */ 6 class Tests_Tax_Query extends WP_UnitTestCase { 7 protected $q; 8 9 function setUp() { 10 parent::setUp(); 11 unset( $this->q ); 12 $this->q = new WP_Query(); 13 } 14 15 function test_category__and_var() { 16 $term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) ); 17 $term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) ); 18 $post_id = $this->factory->post->create(); 19 20 wp_set_post_categories( $post_id, $term_id ); 21 22 $posts = $this->q->query( array( 'category__and' => array( $term_id ) ) ); 23 24 $this->assertEmpty( $this->q->get( 'category__and' ) ); 25 $this->assertCount( 0, $this->q->get( 'category__and' ) ); 26 $this->assertNotEmpty( $this->q->get( 'category__in' ) ); 27 $this->assertCount( 1, $this->q->get( 'category__in' ) ); 28 29 $this->assertNotEmpty( $posts ); 30 $this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) ); 31 32 $posts2 = $this->q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) ); 33 $this->assertNotEmpty( $this->q->get( 'category__and' ) ); 34 $this->assertCount( 2, $this->q->get( 'category__and' ) ); 35 $this->assertEmpty( $this->q->get( 'category__in' ) ); 36 $this->assertCount( 0, $this->q->get( 'category__in' ) ); 37 38 $this->assertEmpty( $posts2 ); 39 } 40 } 41 No newline at end of file -
src/wp-includes/query.php
1762 1762 $q['cat'] = implode(',', $req_cats); 1763 1763 } 1764 1764 1765 if ( !empty($q['category__in']) ) { 1766 $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) ); 1765 if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) { 1766 $q['category__and'] = (array) $q['category__and']; 1767 if ( ! isset( $q['category__in'] ) ) 1768 $q['category__in'] = array(); 1769 $q['category__in'][] = absint( reset( $q['category__and'] ) ); 1770 unset( $q['category__and'] ); 1771 } 1772 1773 if ( ! empty( $q['category__in'] ) ) { 1774 $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) ); 1767 1775 $tax_query[] = array( 1768 1776 'taxonomy' => 'category', 1769 1777 'terms' => $q['category__in'], … … 1772 1780 ); 1773 1781 } 1774 1782 1775 if ( ! empty($q['category__not_in']) ) {1776 $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );1783 if ( ! empty($q['category__not_in']) ) { 1784 $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) ); 1777 1785 $tax_query[] = array( 1778 1786 'taxonomy' => 'category', 1779 1787 'terms' => $q['category__not_in'], … … 1782 1790 ); 1783 1791 } 1784 1792 1785 if ( ! empty($q['category__and']) ) {1786 $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );1793 if ( ! empty($q['category__and']) ) { 1794 $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) ); 1787 1795 $tax_query[] = array( 1788 1796 'taxonomy' => 'category', 1789 1797 'terms' => $q['category__and'],