Ticket #21621: meta-type.diff

File meta-type.diff, 2.1 KB (added by wonderboymusic, 8 months ago)
Line 
1Index: wp-includes/query.php
2===================================================================
3--- wp-includes/query.php       (revision 21825)
4+++ wp-includes/query.php       (working copy)
5@@ -2362,7 +2362,12 @@
6                                                break;
7                                        case $q['meta_key']:
8                                        case 'meta_value':
9-                                               $orderby = "$wpdb->postmeta.meta_value";
10+                                               if ( isset( $q['meta_type'] ) ) {
11+                                                       $meta_type = get_meta_type( $q['meta_type'] );
12+                                                       $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
13+                                               } else {
14+                                                       $orderby = "$wpdb->postmeta.meta_value";
15+                                               }       
16                                                break;
17                                        case 'meta_value_num':
18                                                $orderby = "$wpdb->postmeta.meta_value+0";
19Index: wp-includes/meta.php
20===================================================================
21--- wp-includes/meta.php        (revision 21825)
22+++ wp-includes/meta.php        (working copy)
23@@ -604,6 +604,30 @@
24 }
25 
26 /**
27+ * Given a meta type, return the appropriate alias if applicable
28+ *
29+ * @since 3.5.0
30+ *
31+ * @see WP_Meta_Query
32+ *
33+ * @param string $type MySQL type to cast meta_value
34+ * @return string MySQL type
35+ */
36+function get_meta_type( $type = '' ) {
37+       if ( empty( $type ) )
38+               return 'CHAR';
39+       
40+       $meta_type = strtoupper( $type );
41+
42+       if ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED', 'NUMERIC' ) ) )     
43+               return 'CHAR';
44+                       
45+       if ( 'NUMERIC' == $meta_type )
46+               $meta_type = 'SIGNED';
47+
48+       return $meta_type;
49+}
50+/**
51  * Container class for a multiple metadata query
52  *
53  * @since 3.2.0
54@@ -713,13 +737,8 @@
55 
56                foreach ( $this->queries as $k => $q ) {
57                        $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
58-                       $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
59+                       $meta_type = get_meta_type( isset( $q['type'] ) ? $q['type'] : '' );
60 
61-                       if ( 'NUMERIC' == $meta_type )
62-                               $meta_type = 'SIGNED';
63-                       elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
64-                               $meta_type = 'CHAR';
65-
66                        $meta_value = isset( $q['value'] ) ? $q['value'] : null;
67 
68                        if ( isset( $q['compare'] ) )