When meta_type is passed with orderby => meta_value, orderby must also use CAST() to avoid scenarios like: SELECTing by UNSIGNED and then ordering by CHAR. Adds unit test.

Fixes #21621.

Built from https://develop.svn.wordpress.org/trunk@25255


git-svn-id: http://core.svn.wordpress.org/trunk@25223 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2013-09-05 16:57:08 +00:00
parent 31f26dc3ce
commit 4be84bbdfb
2 changed files with 31 additions and 7 deletions

View File

@ -603,6 +603,30 @@ function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $
return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
}
/**
* Given a meta type, return the appropriate alias if applicable
*
* @since 3.7.0
*
* @see WP_Meta_Query
*
* @param string $type MySQL type to cast meta_value
* @return string MySQL type
*/
function get_meta_type( $type = '' ) {
if ( empty( $type ) )
return 'CHAR';
$meta_type = strtoupper( $type );
if ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED', 'NUMERIC' ) ) )
return 'CHAR';
if ( 'NUMERIC' == $meta_type )
$meta_type = 'SIGNED';
return $meta_type;
}
/**
* Container class for a multiple metadata query
*
@ -744,12 +768,7 @@ class WP_Meta_Query {
foreach ( $queries as $k => $q ) {
$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
if ( 'NUMERIC' == $meta_type )
$meta_type = 'SIGNED';
elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
$meta_type = 'CHAR';
$meta_type = get_meta_type( isset( $q['type'] ) ? $q['type'] : '' );
$meta_value = isset( $q['value'] ) ? $q['value'] : null;

View File

@ -2420,7 +2420,12 @@ class WP_Query {
break;
case $q['meta_key']:
case 'meta_value':
$orderby = "$wpdb->postmeta.meta_value";
if ( isset( $q['meta_type'] ) ) {
$meta_type = get_meta_type( $q['meta_type'] );
$orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
} else {
$orderby = "$wpdb->postmeta.meta_value";
}
break;
case 'meta_value_num':
$orderby = "$wpdb->postmeta.meta_value+0";