Use the primary meta_query clause when parsing orderby in WP_Query.

When using legacy `meta_key`, `meta_value`, etc. arguments in `WP_Query`,
they're converted into the first clause of a `meta_query`. By using that
clause instead of the original arguments, we make sure that behavior is
consistent between the two available formats.

props boonebgorges.
fixes #16814.


git-svn-id: https://develop.svn.wordpress.org/trunk@29855 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Konstantin Kovshenin
2014-10-08 15:11:14 +00:00
parent 6eea081457
commit 061fe13c9f
2 changed files with 60 additions and 8 deletions
+46
View File
@@ -162,11 +162,16 @@ class Tests_Meta extends WP_UnitTestCase {
$this->assertEquals( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
}
/**
* @ticket 16814
*/
function test_meta_type_cast() {
$post_id1 = $this->factory->post->create();
add_post_meta( $post_id1, 'num_as_longtext', 123 );
add_post_meta( $post_id1, 'num_as_longtext_desc', 10 );
$post_id2 = $this->factory->post->create();
add_post_meta( $post_id2, 'num_as_longtext', 99 );
add_post_meta( $post_id2, 'num_as_longtext_desc', 100 );
$posts = new WP_Query( array(
'fields' => 'ids',
@@ -181,6 +186,47 @@ class Tests_Meta extends WP_UnitTestCase {
$this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
$this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
// Make sure the newer meta_query syntax behaves in a consistent way
$posts = new WP_Query( array(
'fields' => 'ids',
'post_type' => 'any',
'meta_query' => array(
array(
'key' => 'num_as_longtext',
'value' => '0',
'compare' => '>',
'type' => 'UNSIGNED',
),
),
'orderby' => 'meta_value',
'order' => 'ASC'
) );
$this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
$this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
// The legacy `meta_key` value should take precedence.
$posts = new WP_Query( array(
'fields' => 'ids',
'post_type' => 'any',
'meta_key' => 'num_as_longtext',
'meta_compare' => '>',
'meta_type' => 'UNSIGNED',
'meta_query' => array(
array(
'key' => 'num_as_longtext_desc',
'value' => '0',
'compare' => '>',
'type' => 'UNSIGNED',
),
),
'orderby' => 'meta_value',
'order' => 'ASC'
) );
$this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
$this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
}
function test_meta_cache_order_asc() {