Changeset 29855
- Timestamp:
- 10/08/2014 03:11:14 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/query.php
r29760 r29855 2224 2224 ); 2225 2225 2226 $meta_key = $this->get( 'meta_key' ); 2227 if ( ! empty( $meta_key ) ) { 2228 $allowed_keys[] = $meta_key; 2226 $primary_meta_key = ''; 2227 $primary_meta_query = false; 2228 if ( ! empty( $this->meta_query->queries ) ) { 2229 $primary_meta_query = reset( $this->meta_query->queries ); 2230 2231 if ( ! empty( $primary_meta_query['key'] ) ) { 2232 $primary_meta_key = $primary_meta_query['key']; 2233 $allowed_keys[] = $primary_meta_key; 2234 } 2235 2229 2236 $allowed_keys[] = 'meta_value'; 2230 2237 $allowed_keys[] = 'meta_value_num'; … … 2251 2258 $orderby = 'RAND()'; 2252 2259 break; 2253 case $ meta_key:2260 case $primary_meta_key: 2254 2261 case 'meta_value': 2255 $type = $this->get( 'meta_type' ); 2256 if ( ! empty( $type ) ) { 2257 $meta_type = $this->meta_query->get_cast_for_type( $type ); 2258 $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})"; 2262 if ( ! empty( $primary_meta_query['type'] ) ) { 2263 $sql_type = $this->meta_query->get_cast_for_type( $primary_meta_query['type'] ); 2264 $orderby = "CAST($wpdb->postmeta.meta_value AS {$sql_type})"; 2259 2265 } else { 2260 2266 $orderby = "$wpdb->postmeta.meta_value"; -
trunk/tests/phpunit/tests/meta.php
r29650 r29855 163 163 } 164 164 165 /** 166 * @ticket 16814 167 */ 165 168 function test_meta_type_cast() { 166 169 $post_id1 = $this->factory->post->create(); 167 170 add_post_meta( $post_id1, 'num_as_longtext', 123 ); 171 add_post_meta( $post_id1, 'num_as_longtext_desc', 10 ); 168 172 $post_id2 = $this->factory->post->create(); 169 173 add_post_meta( $post_id2, 'num_as_longtext', 99 ); 174 add_post_meta( $post_id2, 'num_as_longtext_desc', 100 ); 170 175 171 176 $posts = new WP_Query( array( … … 182 187 $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts ); 183 188 $this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) ); 189 190 // Make sure the newer meta_query syntax behaves in a consistent way 191 $posts = new WP_Query( array( 192 'fields' => 'ids', 193 'post_type' => 'any', 194 'meta_query' => array( 195 array( 196 'key' => 'num_as_longtext', 197 'value' => '0', 198 'compare' => '>', 199 'type' => 'UNSIGNED', 200 ), 201 ), 202 'orderby' => 'meta_value', 203 'order' => 'ASC' 204 ) ); 205 206 $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts ); 207 $this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) ); 208 209 // The legacy `meta_key` value should take precedence. 210 $posts = new WP_Query( array( 211 'fields' => 'ids', 212 'post_type' => 'any', 213 'meta_key' => 'num_as_longtext', 214 'meta_compare' => '>', 215 'meta_type' => 'UNSIGNED', 216 'meta_query' => array( 217 array( 218 'key' => 'num_as_longtext_desc', 219 'value' => '0', 220 'compare' => '>', 221 'type' => 'UNSIGNED', 222 ), 223 ), 224 'orderby' => 'meta_value', 225 'order' => 'ASC' 226 ) ); 227 228 $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts ); 229 $this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) ); 184 230 } 185 231
Note: See TracChangeset
for help on using the changeset viewer.