Make WordPress Core

Ticket #37038: 37038.3.patch

File 37038.3.patch, 3.2 KB (added by spacedmonkey, 9 years ago)
  • src/wp-includes/class-wp-tax-query.php

     
    600600         * @global wpdb $wpdb The WordPress database abstraction object.
    601601         *
    602602         * @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',
    604604         *                                or 'term_id'. Default 'term_id'.
    605605         */
    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'] ) ) {
    610608                        return;
     609                }
    611610
    612                 if ( $query['field'] == $resulting_field )
     611                if ( $query['field'] == $field ) {
    613612                        return;
     613                }
    614614
    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                );
    616624
    617625                switch ( $query['field'] ) {
    618626                        case 'slug':
     627                                $args['slug'] = $terms;
     628                                break;
    619629                        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;
    638631                                break;
    639632                        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;
    646634                                break;
    647635                        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;
    655637                }
    656638
    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 ) {
    658647                        $query = new WP_Error( 'Inexistent terms' );
    659648                        return;
    660649                }
    661650
    662                 $query['terms'] = $terms;
    663                 $query['field'] = $resulting_field;
     651
     652                $query['terms'] = wp_list_pluck( $term_list, $field );
     653                $query['field'] = $field;
    664654        }
    665655}