From 0338c8b3a3133198d76c27fadf8ab28ae3d71131 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 23 Feb 2016 16:39:50 +0000 Subject: [PATCH] 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 --- src/wp-includes/query.php | 12 +++++++++++ tests/phpunit/tests/post/query.php | 33 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 70d02378d9..99df782336 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -1467,6 +1467,7 @@ class WP_Query { * search terms, by prepending a hyphen. * @since 4.5.0 Removed the `$comments_popup` parameter. * Introduced the `$comment_status` and `$ping_status` parameters. + * Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts. * @access public * * @param string|array $query { @@ -1520,6 +1521,7 @@ class WP_Query { * specific `$meta_query` clause, use that clause's array key. * Default 'date'. Accepts 'none', 'name', 'author', 'date', * 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', + * 'RAND(x)' (where 'x' is an integer seed value), * 'comment_count', 'meta_value', 'meta_value_num', 'post__in', * and the array keys of `$meta_query`. * @type int $p Post ID. @@ -2332,6 +2334,14 @@ class WP_Query { $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) ); } + // If RAND() contains a seed value, sanitize and add to allowed keys. + $rand_with_seed = false; + if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) { + $orderby = sprintf( 'RAND(%s)', intval( $matches[1] ) ); + $allowed_keys[] = $orderby; + $rand_with_seed = true; + } + if ( ! in_array( $orderby, $allowed_keys, true ) ) { return false; } @@ -2368,6 +2378,8 @@ class WP_Query { // $orderby corresponds to a meta_query clause. $meta_clause = $meta_clauses[ $orderby ]; $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})"; + } elseif ( $rand_with_seed ) { + $orderby_clause = $orderby; } else { // Default: order by post field. $orderby_clause = "$wpdb->posts.post_" . sanitize_key( $orderby ); diff --git a/tests/phpunit/tests/post/query.php b/tests/phpunit/tests/post/query.php index bc0e865e47..357153847b 100644 --- a/tests/phpunit/tests/post/query.php +++ b/tests/phpunit/tests/post/query.php @@ -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. *