diff --git a/wp-includes/meta.php b/wp-includes/meta.php index ac396a118b..d082c894f1 100644 --- a/wp-includes/meta.php +++ b/wp-includes/meta.php @@ -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; diff --git a/wp-includes/query.php b/wp-includes/query.php index b19b3441e8..cf2f253a32 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -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";