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.