Make WordPress Core

Ticket #15752: 15752.diff

File 15752.diff, 3.4 KB (added by scribu, 14 years ago)

Introduce _set_tax_query_defaults()

  • wp-includes/taxonomy.php

     
    533533        $where = array();
    534534        $i = 0;
    535535
    536         if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {
     536        _set_tax_query_defaults( $tax_query );
     537
     538        if ( strtoupper( $tax_query['relation'] ) == 'OR' ) {
    537539                $relation = 'OR';
    538540        } else {
    539541                $relation = 'AND';
     
    543545                if ( ! is_array( $query ) )
    544546                        continue;
    545547
    546                 extract( wp_parse_args( $query, array(
    547                         'taxonomy' => array(),
    548                         'terms' => array(),
    549                         'include_children' => true,
    550                         'field' => 'term_id',
    551                         'operator' => 'IN',
    552                 ) ) );
     548                extract( $query );
    553549
    554550                $taxonomies = (array) $taxonomy;
    555551
     
    625621        return compact( 'join', 'where' );
    626622}
    627623
     624function _set_tax_query_defaults( &$tax_query ) {
     625        if ( ! isset( $tax_query['relation'] ) )
     626                $tax_query['relation'] = 'AND';
     627
     628        $defaults = array(
     629                'taxonomy' => array(),
     630                'terms' => array(),
     631                'include_children' => true,
     632                'field' => 'term_id',
     633                'operator' => 'IN',
     634        );
     635
     636        foreach ( $tax_query as $i => $query ) {
     637                if ( ! is_array( $query ) )
     638                        continue;
     639
     640                $tax_query[$i] = array_merge( $defaults, $query );
     641
     642                $tax_query[$i]['terms'] = (array) $tax_query[$i]['terms'];
     643        }
     644}
     645
    628646function _transform_terms( &$terms, $taxonomies, $field, $resulting_field ) {
    629647        global $wpdb;
    630648
  • wp-includes/query.php

     
    14911491                                'taxonomy' => $q['taxonomy'],
    14921492                                'terms' => array( $q['term'] ),
    14931493                                'field' => 'slug',
    1494                                 'operator' => 'IN',
    14951494                        );
    14961495                }
    14971496
     
    15001499                                $tax_query_defaults = array(
    15011500                                        'taxonomy' => $taxonomy,
    15021501                                        'field' => 'slug',
    1503                                         'operator' => 'IN'
    15041502                                );
    15051503
    15061504                                if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
     
    15501548                        $tax_query[] = array(
    15511549                                'taxonomy' => 'category',
    15521550                                'terms' => $q['category__in'],
    1553                                 'operator' => 'IN',
    15541551                                'field' => 'term_id'
    15551552                        );
    15561553                }
     
    15611558                                'taxonomy' => 'category',
    15621559                                'terms' => $q['category__not_in'],
    15631560                                'operator' => 'NOT IN',
    1564                                 'field' => 'term_id'
    15651561                        );
    15661562                }
    15671563
     
    15701566                        $tax_query[] = array(
    15711567                                'taxonomy' => 'post_tag',
    15721568                                'terms' => $qv['tag_id'],
    1573                                 'operator' => 'IN',
    1574                                 'field' => 'term_id'
    15751569                        );
    15761570                }
    15771571
     
    15791573                        $tax_query[] = array(
    15801574                                'taxonomy' => 'post_tag',
    15811575                                'terms' => $q['tag__in'],
    1582                                 'operator' => 'IN',
    1583                                 'field' => 'term_id'
    15841576                        );
    15851577                }
    15861578
     
    15891581                                'taxonomy' => 'post_tag',
    15901582                                'terms' => $q['tag__not_in'],
    15911583                                'operator' => 'NOT IN',
    1592                                 'field' => 'term_id'
    15931584                        );
    15941585                }
    15951586
     1587                _set_tax_query_defaults( $tax_query );
     1588
    15961589                foreach ( $tax_query as $query ) {
     1590                        if ( ! is_array( $query ) )
     1591                                continue;
     1592
    15971593                        if ( 'IN' == $query['operator'] ) {
    15981594                                switch ( $query['taxonomy'] ) {
    15991595                                        case 'category':
     
    19451941                // Taxonomies
    19461942                if ( !$this->is_singular ) {
    19471943                        $this->tax_query = $this->parse_tax_query( $q );
    1948                         if ( !empty( $this->tax_query ) ) {
     1944
     1945                        if ( ! empty( $this->tax_query ) ) {
    19491946                                $clauses = call_user_func_array( 'get_tax_sql', array( $this->tax_query, $wpdb->posts, 'ID', &$this) );
    19501947
    19511948                                $join .= $clauses['join'];