Ticket #37038: 37038.4.patch
File 37038.4.patch, 5.5 KB (added by , 4 years ago) |
---|
-
src/wp-includes/class-wp-tax-query.php
604 604 * or 'term_id'. Default 'term_id'. 605 605 */ 606 606 public function transform_query( &$query, $resulting_field ) { 607 global $wpdb; 608 609 if ( empty( $query['terms'] ) ) 607 if ( empty( $query['terms'] ) ) { 610 608 return; 609 } 610 611 611 612 if ( $query['field'] == $resulting_field ) 612 if ( $query['field'] == $resulting_field ){ 613 613 return; 614 } 614 615 615 $resulting_field = sanitize_key( $resulting_field ); 616 $terms = implode( ",", $query['terms'] ); 617 $args = array( 618 'get' => 'all', 619 'number' => false, 620 'taxonomy' => $query['taxonomy'], 621 'update_term_meta_cache' => false, 622 'orderby' => 'none', 623 'suppress_filter' => true, 624 ); 616 625 617 626 switch ( $query['field'] ) { 618 627 case 'slug': 628 $args['slug'] = $terms; 629 break; 619 630 case 'name': 620 foreach ( $query['terms'] as &$term ) { 621 /* 622 * 0 is the $term_id parameter. We don't have a term ID yet, but it doesn't 623 * matter because `sanitize_term_field()` ignores the $term_id param when the 624 * context is 'db'. 625 */ 626 $clean_term = sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ); 627 628 // Match sanitization in wp_insert_term(). 629 $clean_term = wp_unslash( $clean_term ); 630 631 $term = "'" . esc_sql( $clean_term ) . "'"; 632 } 633 634 $terms = implode( ",", $query['terms'] ); 635 636 $terms = $wpdb->get_col( " 637 SELECT $wpdb->term_taxonomy.$resulting_field 638 FROM $wpdb->term_taxonomy 639 INNER JOIN $wpdb->terms USING (term_id) 640 WHERE taxonomy = '{$query['taxonomy']}' 641 AND $wpdb->terms.{$query['field']} IN ($terms) 642 " ); 631 $args['name'] = $terms; 643 632 break; 644 633 case 'term_taxonomy_id': 645 $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); 646 $terms = $wpdb->get_col( " 647 SELECT $resulting_field 648 FROM $wpdb->term_taxonomy 649 WHERE term_taxonomy_id IN ($terms) 650 " ); 634 $args['term_taxonomy_id'] = $terms; 651 635 break; 652 636 default: 653 $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); 654 $terms = $wpdb->get_col( " 655 SELECT $resulting_field 656 FROM $wpdb->term_taxonomy 657 WHERE taxonomy = '{$query['taxonomy']}' 658 AND term_id IN ($terms) 659 " ); 637 $args['include'] = $terms; 660 638 } 661 662 if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { 639 640 $term_list = get_terms( $args ); 641 642 if ( is_wp_error( $term_list ) ) { 643 $query = $term_list; 644 return; 645 } 646 647 if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { 663 648 $query = new WP_Error( 'inexistent_terms', __( 'Inexistent terms.' ) ); 664 649 return; 665 650 } 666 651 667 $query['terms'] = $terms;668 $query['field'] = $resulting_field; 652 $query['terms'] = wp_list_pluck( $term_list, $resulting_field ); 653 $query['field'] = $resulting_field; 669 654 } 670 655 } -
tests/phpunit/tests/query/taxQuery.php
259 259 ), 260 260 ) ); 261 261 262 $this->assertEqual Sets( array( $p1, $p2 ), $q->posts );262 $this->assertEquals( array( $p1, $p2 ), $q->posts ); 263 263 } 264 264 265 265 public function test_tax_query_single_query_multiple_terms_operator_not_in() { … … 280 280 wp_set_post_terms( $p1, $t1, 'category' ); 281 281 wp_set_post_terms( $p2, $t2, 'category' ); 282 282 283 clean_term_cache( array( $t1, $t2 ), 'category' ); 283 284 $q = new WP_Query( array( 284 285 'fields' => 'ids', 285 286 'update_post_meta_cache' => false, … … 359 360 360 361 wp_set_object_terms( $p1, $t1, 'category' ); 361 362 wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); 363 clean_term_cache( array( $t1, $t2 ), 'category' ); 362 364 363 365 $q = new WP_Query( array( 364 366 'fields' => 'ids', … … 373 375 ), 374 376 ), 375 377 ) ); 376 377 378 $this->assertEquals( array( $p2 ), $q->posts ); 378 379 } 379 380 -
tests/phpunit/tests/term/taxQuery.php
146 146 ) ); 147 147 $tq2->transform_query( $tq2->queries[0], 'TERM_ ta%xonomy_id' ); 148 148 149 $this->assert Same( $tq1->queries[0], $tq2->queries[0] );149 $this->assertEquals( $tq1->queries[0], $tq2->queries[0] ); 150 150 } 151 151 152 152 public function test_transform_query_field_slug() { … … 163 163 ) ); 164 164 $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' ); 165 165 166 $this->assert Same( $tt_ids, $tq->queries[0]['terms'] );167 $this->assert Same( 'term_taxonomy_id', $tq->queries[0]['field'] );166 $this->assertEquals( $tt_ids, $tq->queries[0]['terms'] ); 167 $this->assertEquals( 'term_taxonomy_id', $tq->queries[0]['field'] ); 168 168 } 169 169 170 170 public function test_transform_query_field_name() { … … 181 181 ) ); 182 182 $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' ); 183 183 184 $this->assert Same( $tt_ids, $tq->queries[0]['terms'] );185 $this->assert Same( 'term_taxonomy_id', $tq->queries[0]['field'] );184 $this->assertEquals( $tt_ids, $tq->queries[0]['terms'] ); 185 $this->assertEquals( 'term_taxonomy_id', $tq->queries[0]['field'] ); 186 186 } 187 187 188 188 public function test_transform_query_field_term_taxonomy_id() {