diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php index d082c894f1..bec41eec32 100644 --- a/src/wp-includes/meta.php +++ b/src/wp-includes/meta.php @@ -603,30 +603,6 @@ 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 * @@ -712,6 +688,29 @@ class WP_Meta_Query { $this->__construct( $meta_query ); } + /** + * Given a meta type, return the appropriate alias if applicable + * + * @since 3.7.0 + * + * @param string $type MySQL type to cast meta_value + * @return string MySQL type + */ + function get_cast_for_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; + } + /** * Generates SQL clauses to be appended to a main query. * @@ -768,7 +767,7 @@ class WP_Meta_Query { foreach ( $queries as $k => $q ) { $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; - $meta_type = get_meta_type( isset( $q['type'] ) ? $q['type'] : '' ); + $meta_type = $this->get_cast_for_type( isset( $q['type'] ) ? $q['type'] : '' ); $meta_value = isset( $q['value'] ) ? $q['value'] : null; diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index cf2f253a32..329b8d9330 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2421,11 +2421,11 @@ class WP_Query { case $q['meta_key']: case 'meta_value': if ( isset( $q['meta_type'] ) ) { - $meta_type = get_meta_type( $q['meta_type'] ); + $meta_type = $this->meta_query->get_cast_for_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";