Ticket #21621: 21621.2.diff
File 21621.2.diff, 3.1 KB (added by , 11 years ago) |
---|
-
tests/phpunit/tests/meta.php
150 150 $this->assertInternalType( 'int', update_metadata( 'user', $this->author->ID, $key, $value2 ) ); 151 151 $this->assertEquals( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) ); 152 152 } 153 154 function test_meta_type_cast() { 155 $post_id1 = $this->factory->post->create(); 156 add_post_meta( $post_id1, 'num_as_longtext', 123 ); 157 $post_id2 = $this->factory->post->create(); 158 add_post_meta( $post_id2, 'num_as_longtext', 99 ); 159 160 $posts = new WP_Query( array( 161 'fields' => 'ids', 162 'post_type' => 'any', 163 'meta_key' => 'num_as_longtext', 164 'meta_value' => '0', 165 'meta_compare' => '>', 166 'meta_type' => 'UNSIGNED', 167 'orderby' => 'meta_value', 168 'order' => 'ASC' 169 ) ); 170 171 $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts ); 172 $this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) ); 173 } 153 174 } -
src/wp-includes/query.php
2420 2420 break; 2421 2421 case $q['meta_key']: 2422 2422 case 'meta_value': 2423 $orderby = "$wpdb->postmeta.meta_value"; 2423 if ( isset( $q['meta_type'] ) ) { 2424 $meta_type = get_meta_type( $q['meta_type'] ); 2425 $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})"; 2426 } else { 2427 $orderby = "$wpdb->postmeta.meta_value"; 2428 } 2424 2429 break; 2425 2430 case 'meta_value_num': 2426 2431 $orderby = "$wpdb->postmeta.meta_value+0"; -
src/wp-includes/meta.php
604 604 } 605 605 606 606 /** 607 * Given a meta type, return the appropriate alias if applicable 608 * 609 * @since 3.7.0 610 * 611 * @see WP_Meta_Query 612 * 613 * @param string $type MySQL type to cast meta_value 614 * @return string MySQL type 615 */ 616 function get_meta_type( $type = '' ) { 617 if ( empty( $type ) ) 618 return 'CHAR'; 619 620 $meta_type = strtoupper( $type ); 621 622 if ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED', 'NUMERIC' ) ) ) 623 return 'CHAR'; 624 625 if ( 'NUMERIC' == $meta_type ) 626 $meta_type = 'SIGNED'; 627 628 return $meta_type; 629 } 630 /** 607 631 * Container class for a multiple metadata query 608 632 * 609 633 * @since 3.2.0 … … 744 768 745 769 foreach ( $queries as $k => $q ) { 746 770 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; 747 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';771 $meta_type = get_meta_type( isset( $q['type'] ) ? $q['type'] : '' ); 748 772 749 if ( 'NUMERIC' == $meta_type )750 $meta_type = 'SIGNED';751 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )752 $meta_type = 'CHAR';753 754 773 $meta_value = isset( $q['value'] ) ? $q['value'] : null; 755 774 756 775 if ( isset( $q['compare'] ) )