Ticket #37038: 37038.3.patch
File 37038.3.patch, 3.2 KB (added by , 9 years ago) |
---|
-
src/wp-includes/class-wp-tax-query.php
600 600 * @global wpdb $wpdb The WordPress database abstraction object. 601 601 * 602 602 * @param array $query The single query. Passed by reference. 603 * @param string $ resulting_field The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id',603 * @param string $field The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id', 604 604 * or 'term_id'. Default 'term_id'. 605 605 */ 606 public function transform_query( &$query, $resulting_field ) { 607 global $wpdb; 608 609 if ( empty( $query['terms'] ) ) 606 public function transform_query( &$query, $field ) { 607 if ( empty( $query['terms'] ) ) { 610 608 return; 609 } 611 610 612 if ( $query['field'] == $ resulting_field )611 if ( $query['field'] == $field ) { 613 612 return; 613 } 614 614 615 $resulting_field = sanitize_key( $resulting_field ); 615 $terms = implode( ",", $query['terms'] ); 616 $count = count( $terms ); 617 $args = array( 618 'taxonomy' => $query['taxonomy'], 619 'update_term_meta_cache' => false, 620 'hide_empty' => false, 621 'fields' => 'all', 622 'number' => $count 623 ); 616 624 617 625 switch ( $query['field'] ) { 618 626 case 'slug': 627 $args['slug'] = $terms; 628 break; 619 629 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 $term = "'" . esc_sql( sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ) ) . "'"; 627 } 628 629 $terms = implode( ",", $query['terms'] ); 630 631 $terms = $wpdb->get_col( " 632 SELECT $wpdb->term_taxonomy.$resulting_field 633 FROM $wpdb->term_taxonomy 634 INNER JOIN $wpdb->terms USING (term_id) 635 WHERE taxonomy = '{$query['taxonomy']}' 636 AND $wpdb->terms.{$query['field']} IN ($terms) 637 " ); 630 $args['name'] = $terms; 638 631 break; 639 632 case 'term_taxonomy_id': 640 $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); 641 $terms = $wpdb->get_col( " 642 SELECT $resulting_field 643 FROM $wpdb->term_taxonomy 644 WHERE term_taxonomy_id IN ($terms) 645 " ); 633 $args['term_taxonomy_id'] = $terms; 646 634 break; 647 635 default: 648 $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); 649 $terms = $wpdb->get_col( " 650 SELECT $resulting_field 651 FROM $wpdb->term_taxonomy 652 WHERE taxonomy = '{$query['taxonomy']}' 653 AND term_id IN ($terms) 654 " ); 636 $args['include'] = $terms; 655 637 } 656 638 657 if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { 639 $term_list = get_terms( $args ); 640 641 if ( is_wp_error( $term_list ) ) { 642 $query = $term_list; 643 return; 644 } 645 646 if ( 'AND' == $query['operator'] && count( $term_list ) < $count ) { 658 647 $query = new WP_Error( 'Inexistent terms' ); 659 648 return; 660 649 } 661 650 662 $query['terms'] = $terms; 663 $query['field'] = $resulting_field; 651 652 $query['terms'] = wp_list_pluck( $term_list, $field ); 653 $query['field'] = $field; 664 654 } 665 655 }