Move `get_meta_type()` into the `WP_Meta_Query` class as `get_cast_for_type()`. `WP_Query` can then access it like: `$this->meta_query->get_cast_for_type()`.

See #21621, [25255].



git-svn-id: https://develop.svn.wordpress.org/trunk@25269 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2013-09-05 23:31:28 +00:00
parent d765349b75
commit a73d205ab8
2 changed files with 26 additions and 27 deletions

View File

@ -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;

View File

@ -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";