Changeset 40918 for trunk/src/wp-includes/class-wp-tax-query.php
- Timestamp:
- 06/21/2017 03:56:25 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-tax-query.php
r39662 r40918 596 596 * Transforms a single query, from one field to another. 597 597 * 598 * Operates on the `$query` object by reference. In the case of error, 599 * `$query` is converted to a WP_Error object. 600 * 598 601 * @since 3.2.0 599 602 * … … 605 608 */ 606 609 public function transform_query( &$query, $resulting_field ) { 607 global $wpdb;608 609 610 if ( empty( $query['terms'] ) ) 610 611 return; … … 615 616 $resulting_field = sanitize_key( $resulting_field ); 616 617 618 // Empty 'terms' always results in a null transformation. 619 $terms = array_filter( $query['terms'] ); 620 if ( empty( $terms ) ) { 621 $query['terms'] = array(); 622 $query['field'] = $resulting_field; 623 return; 624 } 625 626 $args = array( 627 'get' => 'all', 628 'number' => 0, 629 'taxonomy' => $query['taxonomy'], 630 'update_term_meta_cache' => false, 631 'orderby' => 'none', 632 ); 633 634 // Term query parameter name depends on the 'field' being searched on. 617 635 switch ( $query['field'] ) { 618 636 case 'slug': 637 $args['slug'] = $terms; 638 break; 619 639 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 " ); 640 $args['name'] = $terms; 643 641 break; 644 642 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 " ); 643 $args['term_taxonomy_id'] = $terms; 651 644 break; 652 645 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 " ); 660 } 661 662 if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { 646 $args['include'] = wp_parse_id_list( $terms ); 647 break; 648 } 649 650 $term_query = new WP_Term_Query(); 651 $term_list = $term_query->query( $args ); 652 653 if ( is_wp_error( $term_list ) ) { 654 $query = $term_list; 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.' ) ); 664 660 return; 665 661 } 666 662 667 $query['terms'] = $terms;663 $query['terms'] = wp_list_pluck( $term_list, $resulting_field ); 668 664 $query['field'] = $resulting_field; 669 665 }
Note: See TracChangeset
for help on using the changeset viewer.