Allow querying empty meta values. Props scribu. fixes #15292

git-svn-id: http://svn.automattic.com/wordpress/trunk@17674 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2011-04-21 18:13:03 +00:00
parent 8515d203d3
commit 686c35340b
2 changed files with 13 additions and 10 deletions

View File

@ -387,7 +387,6 @@ function _get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column,
$i = 0; $i = 0;
foreach ( $meta_query as $q ) { foreach ( $meta_query as $q ) {
$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_value = isset( $q['value'] ) ? $q['value'] : '';
$meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '='; $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR'; $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
@ -413,16 +412,20 @@ function _get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column,
if ( !empty( $meta_key ) ) if ( !empty( $meta_key ) )
$where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key ); $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
if ( !isset( $q['value'] ) )
continue;
$meta_value = $q['value'];
if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
if ( ! is_array( $meta_value ) ) if ( ! is_array( $meta_value ) )
$meta_value = preg_split( '/[,\s]+/', $meta_value ); $meta_value = preg_split( '/[,\s]+/', $meta_value );
if ( empty( $meta_value ) )
continue;
} else { } else {
$meta_value = trim( $meta_value ); $meta_value = trim( $meta_value );
} }
if ( empty( $meta_value ) )
continue;
if ( 'IN' == substr( $meta_compare, -2) ) { if ( 'IN' == substr( $meta_compare, -2) ) {
$meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')'; $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
} elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) { } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
@ -435,10 +438,6 @@ function _get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column,
$meta_compare_string = '%s'; $meta_compare_string = '%s';
} }
// @todo Temporary hack to support empty values. Do not use outside of core.
if ( '_wp_zero_value' == $meta_value )
$meta_value = 0;
$where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value ); $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
} }
@ -457,11 +456,15 @@ function _parse_meta_query( &$qv ) {
$meta_query = array(); $meta_query = array();
// Simple query needs to be first for orderby=meta_value to work correctly // Simple query needs to be first for orderby=meta_value to work correctly
foreach ( array( 'key', 'value', 'compare', 'type' ) as $key ) { foreach ( array( 'key', 'compare', 'type' ) as $key ) {
if ( !empty( $qv[ "meta_$key" ] ) ) if ( !empty( $qv[ "meta_$key" ] ) )
$meta_query[0][ $key ] = $qv[ "meta_$key" ]; $meta_query[0][ $key ] = $qv[ "meta_$key" ];
} }
// WP_Query sets 'meta_value' = '' by default
if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
$meta_query[0]['value'] = $qv[ 'meta_value' ];
if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) { if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
$meta_query = array_merge( $meta_query, $qv['meta_query'] ); $meta_query = array_merge( $meta_query, $qv['meta_query'] );
} }

View File

@ -496,7 +496,7 @@ class WP_User_Query {
if ( 'authors' == $qv['who'] && $blog_id ) { if ( 'authors' == $qv['who'] && $blog_id ) {
$qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level'; $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level';
$qv['meta_value'] = '_wp_zero_value'; // Hack to pass '0' $qv['meta_value'] = 0;
$qv['meta_compare'] = '!='; $qv['meta_compare'] = '!=';
$qv['blog_id'] = $blog_id = 0; // Prevent extra meta query $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
} }