[wp-trac] [WordPress Trac] #39140: Taxonomies - operator AND doesn't work properly for taxonomy hierarchies

WordPress Trac noreply at wordpress.org
Tue Jan 8 19:37:11 UTC 2019


#39140: Taxonomies - operator AND doesn't work properly for taxonomy hierarchies
-------------------------------------------------+-------------------------
 Reporter:  bredefladen                          |       Owner:  (none)
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
                                                 |  Review
Component:  Query                                |     Version:  4.6.1
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch needs-refresh has-unit-    |     Focuses:
  tests                                          |
-------------------------------------------------+-------------------------
Changes (by Toro_Unit):

 * keywords:  has-patch => has-patch needs-refresh has-unit-tests


Comment:

 I have same problem. in WP 5.0.

 Posts with parent cateogory not Selected.

 [https://gist.github.com/Nettsidespesialisten/5d4596e18dced3c7d97501f195ffe02e
 bredefladen's change] is Pass the following tests.  But failed some other
 test. So need update patch.

 {{{#!php
 <?php
         public function
 test_tax_query_single_query_multiple_hierarchical_terms_operator_and() {
                 $europe = self::factory()->term->create(
                         array(
                                 'taxonomy' => 'category',
                                 'name'     => 'Europe',
                         )
                 );
                 $spain = self::factory()->term->create(
                         array(
                                 'taxonomy' => 'category',
                                 'name'     => 'Spain',
                                 'parent'   => $europe,
                         )
                 );
                 $south_america = self::factory()->term->create(
                         array(
                                 'taxonomy' => 'category',
                                 'name'     => 'South America',
                         )
                 );
                 $argentina = self::factory()->term->create(
                         array(
                                 'taxonomy' => 'category',
                                 'name'     => 'Argentina',
                                 'parent'   => $south_america,
                         )
                 );
                 $post_a = self::factory()->post->create( array(
 'post_category' => array( $argentina ) ) );
                 $post_b = self::factory()->post->create( array(
 'post_category' => array( $spain ) ) );
                 $post_c = self::factory()->post->create( array(
 'post_category' => array( $argentina, $spain ) ) );

                 $posts = get_posts(
                         array(
                                 'fields'                 => 'ids',
                                 'update_post_meta_cache' => false,
                                 'update_post_term_cache' => false,
                                 'tax_query'              => array(
                                         array(
                                                 'taxonomy' => 'category',
                                                 'field'    => 'id',
                                                 'terms'    => array(
 $argentina, $spain ),
                                                 'operator' => 'AND'
                                         ),
                                 ),
                         )
                 );

                 // ok!
                 $this->assertEquals( 1, count( $posts ) );

                 $posts = get_posts(
                         array(
                                 'fields'                 => 'ids',
                                 'update_post_meta_cache' => false,
                                 'update_post_term_cache' => false,
                                 'tax_query'              => array(
                                         array(
                                                 'taxonomy' => 'category',
                                                 'field'    => 'id',
                                                 'terms'    => array(
 $europe, $south_america ),
                                                 'operator' => 'AND'
                                         ),
                                 ),
                         )
                 );

                 // failed! count( $posts ) = 0!
                 $this->assertEquals( 1, count( $posts ) );

         }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/39140#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list