From ba5cc4e7768f40842ba3834e2785699c7fc7488e Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 23 Feb 2016 16:40:26 +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. Built from https://develop.svn.wordpress.org/trunk@36632 git-svn-id: http://core.svn.wordpress.org/trunk@36599 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/query.php | 12 ++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index 70d02378d9..99df782336 100644 --- a/wp-includes/query.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index b8bcdf4990..be2af7e4b6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.5-alpha-36631'; +$wp_version = '4.5-alpha-36632'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.