Ticket #37038: 37038.5.patch
| File 37038.5.patch, 4.2 KB (added by , 8 years ago) |
|---|
-
src/wp-includes/class-wp-tax-query.php
571 571 } elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) { 572 572 $query = new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 573 573 return; 574 } 574 } 575 575 576 $query['terms'] = array_ unique( (array) $query['terms']);576 $query['terms'] = array_filter( array_unique( (array) $query['terms'] ) ); 577 577 578 578 if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) { 579 579 $this->transform_query( $query, 'term_id' ); … … 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 } 611 610 612 if ( $query['field'] == $resulting_field ) 611 if ( $query['field'] == $resulting_field ) { 613 612 return; 613 } 614 614 615 $terms = $query['terms']; 615 616 $resulting_field = sanitize_key( $resulting_field ); 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; 638 } 639 640 switch ( $resulting_field ) { 641 case 'term_id': 642 $args['fields'] = 'ids'; 643 break; 644 case 'term_taxonomy_id': 645 $args['fields'] = 'tt_ids'; 646 break; 660 647 } 648 $term_query = new WP_Term_Query(); 649 $term_list = $term_query->query( $args ); 650 651 652 if ( is_wp_error( $term_list ) ) { 653 $query = $term_list; 661 654 662 if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { 655 return; 656 } 657 658 if ( 'AND' == $query['operator'] && count( $term_list ) < count( $query['terms'] ) ) { 663 659 $query = new WP_Error( 'inexistent_terms', __( 'Inexistent terms.' ) ); 660 664 661 return; 665 662 } 666 663 667 $query['terms'] = $terms; 664 if ( ! in_array( $resulting_field, array( 'term_id', 'term_taxonomy_id' ) ) ) { 665 $query['terms'] = wp_list_pluck( $term_list, $resulting_field ); 666 } else { 667 $query['terms'] = $term_list; 668 } 669 668 670 $query['field'] = $resulting_field; 669 671 } 670 672 } -
tests/phpunit/tests/query/taxQuery.php
373 373 ), 374 374 ), 375 375 ) ); 376 376 377 377 $this->assertEquals( array( $p2 ), $q->posts ); 378 378 } 379 379