WordPress.org

Make WordPress Core

Changeset 15581


Ignore:
Timestamp:
09/06/2010 11:28:59 AM (9 years ago)
Author:
scribu
Message:

Use _wp_meta_sql() in WP_Query. See #14572. See #14645

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r15580 r15581  
    42554255    $clauses = array();
    42564256
     4257    $join = '';
     4258    $where = '';
     4259    $i = 0;
    42574260    foreach ( $queries as $q ) {
    42584261        $meta_key = isset( $q['meta_key'] ) ? trim( $q['meta_key'] ) : '';
     
    42634266            $meta_compare = '=';
    42644267
    4265         if ( empty( $meta_key ) )
     4268        if ( empty( $meta_key ) && empty( $meta_value ) )
    42664269            continue;
    42674270
    4268         if ( empty( $meta_value ) ) {
    4269             $clauses[ $meta_key ] = "";
    4270         } elseif ( 'like' == $meta_compare ) {
    4271             $clauses[ $meta_key ] = $wpdb->prepare( "LIKE %s", '%' . like_escape( $meta_value ) . '%' );
    4272         } else {
    4273             $clauses[ $meta_key ] = $wpdb->prepare( "$meta_compare %s", $meta_value );
    4274         }
    4275     }
    4276 
    4277     if ( empty( $clauses ) )
    4278         return array('', '');
    4279 
    4280     $join = '';
    4281     $where = '';
    4282 
    4283     $i = 0;
    4284     foreach ( $clauses as $meta_key => $value_query ) {
    42854271        $alias = $i ? 'mt' . $i : $meta_table;
    42864272
     
    42894275        $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
    42904276
    4291         $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
    4292         if ( !empty( $value_query ) )
    4293             $where .= " AND $alias.meta_value $value_query";
    4294            
    42954277        $i++;
     4278
     4279        if ( !empty( $meta_key ) )
     4280            $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
     4281
     4282        if ( empty( $meta_value ) )
     4283            continue;
     4284
     4285        if ( 'like' == $meta_compare ) {
     4286            $where .= $wpdb->prepare( " AND $alias.meta_value LIKE %s", '%' . like_escape( $meta_value ) . '%' );
     4287        } else {
     4288            $where .= $wpdb->prepare( " AND $alias.meta_value $meta_compare %s", $meta_value );
     4289        }
    42964290    }
    42974291
  • trunk/wp-includes/query.php

    r15547 r15581  
    22122212
    22132213        // postmeta queries
    2214         if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
    2215             $join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
    2216         if ( ! empty($q['meta_key']) )
    2217             $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
    2218         if ( ! empty($q['meta_value']) ) {
    2219             if ( empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
    2220                 $q['meta_compare'] = '=';
    2221 
    2222             $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
    2223         }
     2214        $meta_query = wp_array_slice_assoc( $q, array( 'meta_key', 'meta_value', 'meta_compare' ) );
     2215        list( $meta_join, $meta_where ) = _wp_meta_sql( array( $meta_query ), $wpdb->posts, 'ID', $wpdb->postmeta, 'post_id' );
     2216        $join .= $meta_join;
     2217        $where .= $meta_where;
    22242218
    22252219        // Apply filters on where and join prior to paging so that any
Note: See TracChangeset for help on using the changeset viewer.