diff --git src/wp-includes/query.php src/wp-includes/query.php
index a832e6d..dc9237f 100644
|
|
class WP_Query { |
2223 | 2223 | 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand', |
2224 | 2224 | ); |
2225 | 2225 | |
2226 | | $meta_key = $this->get( 'meta_key' ); |
2227 | | if ( ! empty( $meta_key ) ) { |
2228 | | $allowed_keys[] = $meta_key; |
| 2226 | $meta_key = ''; |
| 2227 | $primary_meta_query = false; |
| 2228 | if ( ! empty( $this->meta_query->queries ) ) { |
| 2229 | $meta_query = reset( $this->meta_query->queries ); |
| 2230 | |
| 2231 | if ( ! empty( $meta_query['key'] ) ) { |
| 2232 | $meta_key = $meta_query['key']; |
| 2233 | $allowed_keys[] = $meta_query['key']; |
| 2234 | } |
| 2235 | |
2229 | 2236 | $allowed_keys[] = 'meta_value'; |
2230 | 2237 | $allowed_keys[] = 'meta_value_num'; |
2231 | 2238 | } |
… |
… |
class WP_Query { |
2252 | 2259 | break; |
2253 | 2260 | case $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 ); |
| 2262 | if ( ! empty( $meta_query['type'] ) ) { |
| 2263 | $meta_type = $this->meta_query->get_cast_for_type( $meta_query['type'] ); |
2258 | 2264 | $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})"; |
2259 | 2265 | } else { |
2260 | 2266 | $orderby = "$wpdb->postmeta.meta_value"; |
diff --git tests/phpunit/tests/meta.php tests/phpunit/tests/meta.php
index 8da40de..abf3ba5 100644
|
|
class Tests_Meta extends WP_UnitTestCase { |
162 | 162 | $this->assertEquals( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) ); |
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( |
172 | 177 | 'fields' => 'ids', |
… |
… |
class Tests_Meta extends WP_UnitTestCase { |
181 | 186 | |
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 | |
186 | 232 | function test_meta_cache_order_asc() { |