[wp-trac] [WordPress Trac] #47802: WP_Query get_posts sometimes inserts contradictory taxonomy queries

WordPress Trac noreply at wordpress.org
Fri Sep 4 15:17:24 UTC 2020


#47802: WP_Query get_posts sometimes inserts contradictory taxonomy queries
--------------------------+------------------------------
 Reporter:  nstevensdc    |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Query         |     Version:
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by krypsin):

 I believe this is an issue that I'm also experiencing.

 The thing that I've noticed is that the query works when using `term_id`
 as the `field` value but not `slug`.
 Using `slug` is what seems to be adding the additional `AND` is coming
 from.

 So, the following (using `slug`) does not work:

 Args dump:
 {{{
 array(3) {
   ["course_type"]=>
   array(1) {
     [0]=>
     array(4) {
       ["taxonomy"]=>
       string(11) "course_type"
       ["field"]=>
       string(4) "slug"
       ["terms"]=>
       array(2) {
         [0]=>
         string(30) "coursetype1"
         [1]=>
         string(20) "coursetype2"
       }
       ["compare"]=>
       string(2) "IN"
     }
   }
   ["location"]=>
   array(1) {
     [0]=>
     array(4) {
       ["taxonomy"]=>
       string(8) "location"
       ["field"]=>
       string(4) "slug"
       ["terms"]=>
       array(1) {
         [0]=>
         string(23) "location1"
       }
       ["compare"]=>
       string(2) "IN"
     }
   }
   ["relation"]=>
   string(3) "AND"
 }
 }}}
 SQL:
 {{{
 SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  LEFT JOIN
 wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
 LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)
 LEFT JOIN wp_term_relationships AS tt2 ON (wp_posts.ID = tt2.object_id)
 WHERE 1=1 AND (
     (
         wp_term_relationships.term_taxonomy_id IN (650,929)
     )
     AND
     (
         tt1.term_taxonomy_id IN (1059)
     )
     AND
     tt2.term_taxonomy_id IN (929)
 ) AND wp_posts.post_type = 'course' AND ((wp_posts.post_status =
 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0,
 10
 }}}

 Whereas using `term_id` does:

 Args dump:
 {{{
 array(3) {
   ["course_type"]=>
   array(1) {
     [0]=>
     array(4) {
       ["taxonomy"]=>
       string(11) "course_type"
       ["field"]=>
       string(7) "term_id"
       ["terms"]=>
       array(2) {
         [0]=>
         string(3) "929"
         [1]=>
         string(3) "650"
       }
       ["compare"]=>
       string(2) "IN"
     }
   }
   ["location"]=>
   array(1) {
     [0]=>
     array(4) {
       ["taxonomy"]=>
       string(8) "location"
       ["field"]=>
       string(7) "term_id"
       ["terms"]=>
       array(1) {
         [0]=>
         string(4) "1059"
       }
       ["compare"]=>
       string(2) "IN"
     }
   }
   ["relation"]=>
   string(3) "AND"
 }
 }}}

 SQL
 {{{
 SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  LEFT JOIN
 wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
 LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)
 WHERE 1=1 AND (
     (
         wp_term_relationships.term_taxonomy_id IN (650,929)
     )
         AND
     (
         tt1.term_taxonomy_id IN (1059)
     )
 ) AND wp_posts.post_type = 'course' AND ((wp_posts.post_status =
 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0,
 10
 }}}

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


More information about the wp-trac mailing list