Query: Allow a seed value to be passed when using 'rand' $orderby.

`WP_Query` allows random ordering; `'orderby' => 'rand'` translates to
`ORDER BY RAND()`. This syntax results in random ordering that is not
consistent from request to request. MySQL supports the passing of a seed value
to random sorts, such as `ORDER BY RAND(3)`, which will return the same
random value each time it's called. `WP_Query` now supports this syntax, by
passing `RAND(3)` (or whatever integer seed value you'd like) as the value
of `'orderby'`.

Props hlashbrooke.
Fixes #35692.

git-svn-id: https://develop.svn.wordpress.org/trunk@36632 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-02-23 16:39:50 +00:00
parent 7b1c4151dd
commit 0338c8b3a3
2 changed files with 45 additions and 0 deletions
+33
View File
@@ -303,6 +303,39 @@ class Tests_Post_Query extends WP_UnitTestCase {
$this->assertNotContains( 'ASC', $q5->request );
}
/**
* @ticket 35692
*/
public function test_orderby_rand_with_seed() {
$q = new WP_Query( array(
'orderby' => 'RAND(5)',
) );
$this->assertContains( 'ORDER BY RAND(5)', $q->request );
}
/**
* @ticket 35692
*/
public function test_orderby_rand_should_ignore_invalid_seed() {
$q = new WP_Query( array(
'orderby' => 'RAND(foo)',
) );
$this->assertNotContains( 'ORDER BY RAND', $q->request );
}
/**
* @ticket 35692
*/
public function test_orderby_rand_with_seed_should_be_case_insensitive() {
$q = new WP_Query( array(
'orderby' => 'rand(5)',
) );
$this->assertContains( 'ORDER BY RAND(5)', $q->request );
}
/**
* Tests the post_name__in attribute of WP_Query.
*