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
This commit is contained in:
parent
710be9d2ba
commit
ba5cc4e776
|
@ -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 );
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue