git-svn-id: http://svn.automattic.com/wordpress/trunk@15581 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
c27d704999
commit
80e2918946
|
@ -4254,6 +4254,9 @@ function _wp_meta_sql( $queries, $primary_table, $primary_id_column, $meta_table
|
|||
|
||||
$clauses = array();
|
||||
|
||||
$join = '';
|
||||
$where = '';
|
||||
$i = 0;
|
||||
foreach ( $queries as $q ) {
|
||||
$meta_key = isset( $q['meta_key'] ) ? trim( $q['meta_key'] ) : '';
|
||||
$meta_value = isset( $q['meta_value'] ) ? trim( $q['meta_value'] ) : '';
|
||||
|
@ -4262,37 +4265,28 @@ function _wp_meta_sql( $queries, $primary_table, $primary_id_column, $meta_table
|
|||
if ( !in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'like' ) ) )
|
||||
$meta_compare = '=';
|
||||
|
||||
if ( empty( $meta_key ) )
|
||||
if ( empty( $meta_key ) && empty( $meta_value ) )
|
||||
continue;
|
||||
|
||||
if ( empty( $meta_value ) ) {
|
||||
$clauses[ $meta_key ] = "";
|
||||
} elseif ( 'like' == $meta_compare ) {
|
||||
$clauses[ $meta_key ] = $wpdb->prepare( "LIKE %s", '%' . like_escape( $meta_value ) . '%' );
|
||||
} else {
|
||||
$clauses[ $meta_key ] = $wpdb->prepare( "$meta_compare %s", $meta_value );
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $clauses ) )
|
||||
return array('', '');
|
||||
|
||||
$join = '';
|
||||
$where = '';
|
||||
|
||||
$i = 0;
|
||||
foreach ( $clauses as $meta_key => $value_query ) {
|
||||
$alias = $i ? 'mt' . $i : $meta_table;
|
||||
|
||||
$join .= "\nINNER JOIN $meta_table";
|
||||
$join .= $i ? " AS $alias" : '';
|
||||
$join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
|
||||
|
||||
$where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
|
||||
if ( !empty( $value_query ) )
|
||||
$where .= " AND $alias.meta_value $value_query";
|
||||
|
||||
$i++;
|
||||
|
||||
if ( !empty( $meta_key ) )
|
||||
$where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
|
||||
|
||||
if ( empty( $meta_value ) )
|
||||
continue;
|
||||
|
||||
if ( 'like' == $meta_compare ) {
|
||||
$where .= $wpdb->prepare( " AND $alias.meta_value LIKE %s", '%' . like_escape( $meta_value ) . '%' );
|
||||
} else {
|
||||
$where .= $wpdb->prepare( " AND $alias.meta_value $meta_compare %s", $meta_value );
|
||||
}
|
||||
}
|
||||
|
||||
return array( $join, $where );
|
||||
|
|
|
@ -2211,16 +2211,10 @@ class WP_Query {
|
|||
}
|
||||
|
||||
// postmeta queries
|
||||
if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
|
||||
$join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
|
||||
if ( ! empty($q['meta_key']) )
|
||||
$where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
|
||||
if ( ! empty($q['meta_value']) ) {
|
||||
if ( empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
|
||||
$q['meta_compare'] = '=';
|
||||
|
||||
$where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
|
||||
}
|
||||
$meta_query = wp_array_slice_assoc( $q, array( 'meta_key', 'meta_value', 'meta_compare' ) );
|
||||
list( $meta_join, $meta_where ) = _wp_meta_sql( array( $meta_query ), $wpdb->posts, 'ID', $wpdb->postmeta, 'post_id' );
|
||||
$join .= $meta_join;
|
||||
$where .= $meta_where;
|
||||
|
||||
// Apply filters on where and join prior to paging so that any
|
||||
// manipulations to them are reflected in the paging by day queries.
|
||||
|
|
Loading…
Reference in New Issue