diff --git src/wp-includes/class-wp-tax-query.php src/wp-includes/class-wp-tax-query.php
index b46aede70a..b5f24a263d 100644
|
|
|
class WP_Tax_Query { |
| 595 | 595 | /** |
| 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 | * |
| 600 | 603 | * @global wpdb $wpdb The WordPress database abstraction object. |
| … |
… |
class WP_Tax_Query { |
| 604 | 607 | * or 'term_id'. Default 'term_id'. |
| 605 | 608 | */ |
| 606 | 609 | public function transform_query( &$query, $resulting_field ) { |
| 607 | | global $wpdb; |
| 608 | | |
| 609 | 610 | if ( empty( $query['terms'] ) ) |
| 610 | 611 | return; |
| 611 | 612 | |
| … |
… |
class WP_Tax_Query { |
| 614 | 615 | |
| 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 | | " ); |
| | 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; |
| 660 | 656 | } |
| 661 | 657 | |
| 662 | | if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { |
| | 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 | } |
| 670 | 666 | } |
diff --git tests/phpunit/tests/term/taxQuery.php tests/phpunit/tests/term/taxQuery.php
index 179d25123d..1aacaca67b 100644
|
|
|
class Tests_Term_Tax_Query extends WP_UnitTestCase { |
| 163 | 163 | ) ); |
| 164 | 164 | $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' ); |
| 165 | 165 | |
| 166 | | $this->assertSame( $tt_ids, $tq->queries[0]['terms'] ); |
| | 166 | $this->assertEqualSets( $tt_ids, $tq->queries[0]['terms'] ); |
| 167 | 167 | $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] ); |
| 168 | 168 | } |
| 169 | 169 | |
| … |
… |
class Tests_Term_Tax_Query extends WP_UnitTestCase { |
| 181 | 181 | ) ); |
| 182 | 182 | $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' ); |
| 183 | 183 | |
| 184 | | $this->assertSame( $tt_ids, $tq->queries[0]['terms'] ); |
| | 184 | $this->assertEqualSets( $tt_ids, $tq->queries[0]['terms'] ); |
| 185 | 185 | $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] ); |
| 186 | 186 | } |
| 187 | 187 | |