Ticket #37038: 37038.2.patch
File 37038.2.patch, 5.6 KB (added by , 8 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_fieldThe resulting field. Accepts 'slug', 'name', 'term_taxonomy_id',603 * @param string $fields 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) {606 public function transform_query( &$query, $fields ) { 607 607 global $wpdb; 608 608 609 if ( empty( $query['terms'] ) ) 609 if ( empty( $query['terms'] ) ) { 610 610 return; 611 } 611 612 612 if ( $query['field'] == $ resulting_field )613 if ( $query['field'] == $fields ) { 613 614 return; 615 } 614 616 615 $ resulting_field = sanitize_key( $resulting_field);617 $fields = sanitize_key( $fields ); 616 618 619 $args = array( 620 'taxonomy' => $query['taxonomy'], 621 'update_term_meta_cache' => false, 622 'hide_empty' => false, 623 'fields' => $fields 624 ); 625 617 626 switch ( $query['field'] ) { 618 627 case 'slug': 628 foreach ( $query['terms'] as &$term ) { 629 /* 630 * 0 is the $term_id parameter. We don't have a term ID yet, but it doesn't 631 * matter because `sanitize_term_field()` ignores the $term_id param when the 632 * context is 'db'. 633 */ 634 $term = "'" . esc_sql( sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ) ) . "'"; 635 } 636 637 $terms = implode( ",", $query['terms'] ); 638 $args['slug'] = $terms; 639 break; 619 640 case 'name': 620 641 foreach ( $query['terms'] as &$term ) { 621 642 /* … … 626 647 $term = "'" . esc_sql( sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ) ) . "'"; 627 648 } 628 649 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 " ); 650 $terms = implode( ",", $query['terms'] ); 651 $args['name'] = $terms; 638 652 break; 639 653 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 " ); 654 $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); 655 $args['term_taxonomy_id'] = $terms; 646 656 break; 647 657 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 " ); 658 $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); 659 $args['include'] = $terms; 655 660 } 661 $args['number'] = count( $terms ); 662 $term_list = get_terms( $args ); 656 663 657 if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { 664 if ( is_wp_error( $term_list ) ) { 665 $query = $term_list; 666 return; 667 } 668 669 if ( 'AND' == $query['operator'] && count( $term_list ) < count( $query['terms'] ) ) { 658 670 $query = new WP_Error( 'Inexistent terms' ); 659 671 return; 660 672 } 661 673 662 $query['terms'] = $term s;663 $query['field'] = $ resulting_field;674 $query['terms'] = $term_list; 675 $query['field'] = $fields; 664 676 } 665 677 } -
src/wp-includes/class-wp-term-query.php
183 183 'count' => false, 184 184 'name' => '', 185 185 'slug' => '', 186 'term_taxonomy_id' => '', 186 187 'hierarchical' => true, 187 188 'search' => '', 188 189 'name__like' => '', … … 473 474 } 474 475 } 475 476 477 if ( ! empty( $args['term_taxonomy_id'] ) ) { 478 if ( is_array( $args['term_taxonomy_id'] ) ) { 479 $term_taxonomy_id = array_map( 'intval', $args['term_taxonomy_id'] ); 480 $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ('" . implode( "', '", $term_taxonomy_id ) . "')"; 481 } else { 482 $term_taxonomy_id = intval( $args['term_taxonomy_id'] ); 483 $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id = '$term_taxonomy_id'"; 484 } 485 } 486 476 487 if ( ! empty( $args['name__like'] ) ) { 477 488 $this->sql_clauses['where']['name__like'] = $wpdb->prepare( "t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' ); 478 489 } … … 541 552 case 'names': 542 553 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy' ); 543 554 break; 555 case 'term_taxonomy_id': 556 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 'tt.term_taxonomy_id', 'tt.taxonomy' ); 557 break; 544 558 case 'count': 545 559 $orderby = ''; 546 560 $order = ''; … … 692 706 foreach ( $terms as $term ) { 693 707 $_terms[] = $term->term_id; 694 708 } 695 } elseif ( ' names' == $_fields ) {709 } elseif ( 'term_taxonomy_id' == $_fields ) { 696 710 foreach ( $terms as $term ) { 711 $_terms[] = $term->term_taxonomy_id; 712 } 713 }elseif ( 'names' == $_fields ) { 714 foreach ( $terms as $term ) { 697 715 $_terms[] = $term->name; 698 716 } 699 717 } elseif ( 'id=>name' == $_fields ) {