Commit 52b5bd1522 for wordpress.org
commit 52b5bd15225abc686e1f6350744881c943b09bbc
Author: jorbin <jorbin@git.wordpress.org>
Date: Tue Nov 17 04:53:59 2026 +0000
Query: Select random redirect posts by random post ID.
Change Random content requests from using `ORDER BY RAND() LIMIT 1` to using a sampling with rejection method. The post is now selected by looking up random post IDs by primary key, which keeps the cost of a request independent of the number of posts while leaving every eligible post equally likely to be selected.
Archives whose posts are too sparse among the post IDs to be found this way fall back to a pool of randomly ordered posts stored in the object cache.
Developed in: https://github.com/WordPress/wordpress-develop/pull/16050
Follow-up to r64402, r65042.
Props jorbin, peterwilsoncc, sukhendu2002, rmccue, gddrt, renehermi, djpaul.
Fixes #66654. See #64498.
Built from https://develop.svn.wordpress.org/trunk@65542
git-svn-id: http://core.svn.wordpress.org/trunk@64615 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/class-wp.php b/wp-includes/class-wp.php
index 68c4a51ad7..5c9b33a77a 100644
--- a/wp-includes/class-wp.php
+++ b/wp-includes/class-wp.php
@@ -707,7 +707,9 @@ class WP {
* Redirects random content requests to a randomly selected post.
*
* For random content requests, runs the main query to select a single random post
- * from the requested archive and redirects to it. For example, `example.com/random/`
+ * from the requested archive and redirects to it. The post is selected by looking up
+ * random post IDs, see {@see wp_random_content_posts_pre_query()}, as sorting posts
+ * randomly requires the database to read every eligible post. For example, `example.com/random/`
* redirects to a random post and `example.com/random/category/news/` redirects to a
* random post in the "news" category.
*
@@ -716,8 +718,11 @@ class WP {
* enforcing the eligible posts, order and limit are added after any other callbacks:
*
* - {@see wp_random_content_pre_get_posts()} on the {@see 'pre_get_posts'} action.
+ * - {@see wp_random_content_posts_where()} on the {@see 'posts_where'} filter.
* - {@see wp_random_content_posts_orderby()} on the {@see 'posts_orderby'} filter.
* - {@see wp_random_content_post_limits()} on the {@see 'post_limits'} filter.
+ * - {@see wp_random_content_posts_fields()} on the {@see 'posts_fields'} filter.
+ * - {@see wp_random_content_posts_pre_query()} on the {@see 'posts_pre_query'} filter.
*
* If a post is selected, the redirect is sent by {@see wp_random_content_redirect()}
* on the {@see 'template_redirect'} action at priority 100, so the request is set up
@@ -771,14 +776,20 @@ class WP {
}
add_action( 'pre_get_posts', 'wp_random_content_pre_get_posts', 20 );
+ add_filter( 'posts_where', 'wp_random_content_posts_where', 20, 2 );
add_filter( 'posts_orderby', 'wp_random_content_posts_orderby', 20, 2 );
add_filter( 'post_limits', 'wp_random_content_post_limits', 20, 2 );
+ add_filter( 'posts_fields', 'wp_random_content_posts_fields', 20, 2 );
+ add_filter( 'posts_pre_query', 'wp_random_content_posts_pre_query', 20, 2 );
$this->query_posts();
remove_action( 'pre_get_posts', 'wp_random_content_pre_get_posts', 20 );
+ remove_filter( 'posts_where', 'wp_random_content_posts_where', 20 );
remove_filter( 'posts_orderby', 'wp_random_content_posts_orderby', 20 );
remove_filter( 'post_limits', 'wp_random_content_post_limits', 20 );
+ remove_filter( 'posts_fields', 'wp_random_content_posts_fields', 20 );
+ remove_filter( 'posts_pre_query', 'wp_random_content_posts_pre_query', 20 );
if ( ! wp_is_random_content_query( $wp_the_query ) ) {
return true;
diff --git a/wp-includes/query.php b/wp-includes/query.php
index 70a6bd4c0d..8256ef2285 100644
--- a/wp-includes/query.php
+++ b/wp-includes/query.php
@@ -1400,6 +1400,9 @@ function wp_get_random_content_post_types( WP_Query $query ): array {
* {@see wp_random_content_posts_orderby()}, so callbacks modifying them on archives do not
* affect the selection.
*
+ * The random post IDs the post is selected from are stored in the `random_sample_ids`
+ * query variable, see {@see wp_get_random_content_sample_ids()}.
+ *
* Added to the {@see 'pre_get_posts'} action by {@see WP::handle_random()} while the
* random content query runs, so it runs after other callbacks.
*
@@ -1415,6 +1418,7 @@ function wp_random_content_pre_get_posts( $query ): void {
$query->set( 'post_type', wp_get_random_content_post_types( $query ) );
$query->set( 'post_status', 'publish' );
$query->set( 'has_password', false );
+ $query->set( 'random_sample_ids', wp_get_random_content_sample_ids() );
$query->set( 'ignore_sticky_posts', true );
$query->set( 'no_found_rows', true );
$query->set( 'update_post_meta_cache', false );
@@ -1424,7 +1428,96 @@ function wp_random_content_pre_get_posts( $query ): void {
}
/**
- * Orders the random content query randomly.
+ * Retrieves random post IDs for the random content query to select a post from.
+ *
+ * Sorting posts randomly requires the database to read every eligible post. Instead,
+ * the random content query looks up 50 random post IDs, each by the primary key, and
+ * selects one of those that is an eligible post. The cost of the lookup does not
+ * depend on the number of posts, and as every post ID is equally likely to be looked
+ * up, every eligible post is equally likely to be selected. This is the case whether
+ * or not there are gaps between the IDs of posts.
+ *
+ * The IDs are between 1 and the highest post ID, which is stored in the object cache
+ * for 10 minutes. If there are no more than 50 post IDs, all of them are returned.
+ *
+ * @since 7.2.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @return int[] Up to 50 distinct post IDs. Posts with the IDs may not exist.
+ *
+ * @phpstan-return non-empty-list<int>
+ */
+function wp_get_random_content_sample_ids(): array {
+ global $wpdb;
+
+ $max_id = wp_cache_get( 'max_post_id', 'random-content' );
+
+ if ( ! is_int( $max_id ) ) {
+ $max_id = (int) $wpdb->get_var( "SELECT MAX(ID) FROM {$wpdb->posts}" );
+ wp_cache_set( 'max_post_id', $max_id, 'random-content', 10 * MINUTE_IN_SECONDS );
+ }
+
+ if ( $max_id <= 50 ) {
+ return range( 1, max( 1, $max_id ) );
+ }
+
+ $post_ids = array();
+
+ while ( count( $post_ids ) < 50 ) {
+ $post_ids[ wp_rand( 1, $max_id ) ] = true;
+ }
+
+ return array_keys( $post_ids );
+}
+
+/**
+ * Retrieves the SQL clause limiting the random content query to random post IDs.
+ *
+ * @since 7.2.0
+ * @access private
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param int[] $post_ids Post IDs.
+ * @return string SQL clause.
+ */
+function _wp_random_content_sample_clause( array $post_ids ): string {
+ global $wpdb;
+
+ return "{$wpdb->posts}.ID IN (" . implode( ',', array_map( 'intval', $post_ids ) ) . ')';
+}
+
+/**
+ * Limits the random content query to the random post IDs.
+ *
+ * Added to the {@see 'posts_where'} filter by {@see WP::handle_random()} while the random
+ * content query runs.
+ *
+ * @since 7.2.0
+ *
+ * @see wp_get_random_content_sample_ids()
+ *
+ * @param string $where The WHERE clause of the query.
+ * @param WP_Query $query The WP_Query instance.
+ * @return string The WHERE clause of the query.
+ */
+function wp_random_content_posts_where( $where, WP_Query $query ): string {
+ $sample_ids = $query->get( 'random_sample_ids' );
+
+ if ( wp_is_random_content_query( $query ) && is_array( $sample_ids ) && $sample_ids ) {
+ $where .= ' AND ' . _wp_random_content_sample_clause( $sample_ids );
+ }
+
+ return (string) $where;
+}
+
+/**
+ * Removes the order of the random content query.
+ *
+ * The random content query is limited to random post IDs, one of which is selected
+ * once the query has run, see {@see wp_random_content_posts_pre_query()}. Ordering the
+ * posts would require the database to sort them.
*
* Added to the {@see 'posts_orderby'} filter by {@see WP::handle_random()} while the random
* content query runs. Enforcing the order in SQL prevents {@see 'pre_get_posts'}
@@ -1438,14 +1531,17 @@ function wp_random_content_pre_get_posts( $query ): void {
*/
function wp_random_content_posts_orderby( $orderby, WP_Query $query ): string {
if ( wp_is_random_content_query( $query ) ) {
- return 'RAND()';
+ return '';
}
return (string) $orderby;
}
/**
- * Limits the random content query to a single post.
+ * Removes the limit of the random content query.
+ *
+ * The random content query is limited to random post IDs, one of which is selected
+ * once the query has run, see {@see wp_random_content_posts_pre_query()}.
*
* Added to the {@see 'post_limits'} filter by {@see WP::handle_random()} while the random
* content query runs. Enforcing the limit in SQL prevents {@see 'pre_get_posts'}
@@ -1460,12 +1556,247 @@ function wp_random_content_posts_orderby( $orderby, WP_Query $query ): string {
*/
function wp_random_content_post_limits( $limits, WP_Query $query ): string {
if ( wp_is_random_content_query( $query ) ) {
- return 'LIMIT 0, 1';
+ return '';
}
return (string) $limits;
}
+/**
+ * Limits the random content query to selecting post IDs.
+ *
+ * Added to the {@see 'posts_fields'} filter by {@see WP::handle_random()} while the random
+ * content query runs.
+ *
+ * @since 7.2.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param string $fields The SELECT clause of the query.
+ * @param WP_Query $query The WP_Query instance.
+ * @return string The SELECT clause of the query.
+ */
+function wp_random_content_posts_fields( $fields, WP_Query $query ): string {
+ global $wpdb;
+
+ if ( wp_is_random_content_query( $query ) ) {
+ return "{$wpdb->posts}.ID";
+ }
+
+ return (string) $fields;
+}
+
+/**
+ * Selects the random post from the eligible posts among random post IDs.
+ *
+ * The random content query is limited to 50 random post IDs, see
+ * {@see wp_get_random_content_sample_ids()}, and one of the posts it selects is chosen
+ * at random. If none of the IDs are eligible posts, the query is run again with other
+ * random post IDs, up to three times in total. On sites where most post IDs are
+ * eligible posts, a post is nearly always selected by the first query.
+ *
+ * If no post is selected, eligible posts are too sparse among the post IDs to be found
+ * this way, for example a small category on a site with many posts. A pool of up to
+ * 1,000 randomly ordered eligible posts is then selected, which is the whole archive
+ * if it has no more posts than that, and their IDs are stored in the object cache for
+ * 10 minutes. While the pool is cached, the post is selected from it without querying
+ * the database for the eligible posts. Selecting the pool requires the database to
+ * read every eligible post, so sites with a persistent object cache do so at most once
+ * every 10 minutes for each such archive.
+ *
+ * The pool is selected without excluding password protected posts, which are skipped
+ * when the post is selected from it, so the database can read the eligible posts from
+ * an index rather than reading each post in full.
+ *
+ * Only one request selects the pool at a time. Once the pool is 10 minutes old, the
+ * first request to need it selects the next pool and, until it has, other requests
+ * select from the previous pool, which is kept for an hour. If there is no previous
+ * pool, as the archive has not been requested recently, concurrent requests each
+ * select a pool.
+ *
+ * The pool is keyed by the SQL of the random content query, excluding the random post
+ * IDs, so each archive, and each variation of an archive produced by
+ * {@see 'pre_get_posts'} callbacks or query filters, has its own pool.
+ *
+ * As the pool may be up to 10 minutes old, posts that have since been unpublished,
+ * deleted, password protected or changed to another post type are skipped. If none of
+ * its posts are eligible, a post is selected as if there was no pool. Other changes,
+ * such as a post being published or removed from a term, are reflected once the pool
+ * expires. This includes the empty pool of an archive without eligible posts, which
+ * is cached so the archive is not queried for every request.
+ *
+ * Added to the {@see 'posts_pre_query'} filter by {@see WP::handle_random()} while the
+ * random content query runs. The database is only read from, never written to.
+ *
+ * @since 7.2.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param WP_Post[]|int[]|null $posts Posts short-circuiting the query, or null to run the query.
+ * @param WP_Query $query The WP_Query instance.
+ * @return WP_Post[]|int[]|null The selected post, an empty array if no post is eligible,
+ * or the unmodified value if the query is not for random content.
+ */
+function wp_random_content_posts_pre_query( $posts, WP_Query $query ) {
+ global $wpdb;
+
+ if ( null !== $posts || ! wp_is_random_content_query( $query ) || ! is_string( $query->request ) ) {
+ return $posts;
+ }
+
+ // The placeholder escape string is unique to each request, see wpdb::placeholder_escape().
+ $request = $wpdb->remove_placeholder_escape( $query->request );
+ $sample_ids = $query->get( 'random_sample_ids' );
+ $sample_clause = is_array( $sample_ids ) && $sample_ids ? _wp_random_content_sample_clause( $sample_ids ) : '';
+
+ /*
+ * The random post IDs can only be replaced, to look up other IDs or to select the
+ * pool, if another callback has not modified their clause.
+ */
+ $can_resample = '' !== $sample_clause && 1 === substr_count( $request, $sample_clause );
+ $cache_key = $can_resample ? 'pool:' . md5( str_replace( $sample_clause, '', $request ) ) : '';
+ $post_types = (array) $query->get( 'post_type' );
+
+ /**
+ * Selects a random eligible post from post IDs.
+ *
+ * @param array<int|string> $post_ids Post IDs.
+ * @return WP_Post|null The selected post, or null if none of the posts are eligible.
+ */
+ $select_post = static function ( array $post_ids ) use ( $post_types ): ?WP_Post {
+ shuffle( $post_ids );
+
+ foreach ( $post_ids as $post_id ) {
+ $post = get_post( (int) $post_id );
+
+ if (
+ $post instanceof WP_Post
+ && 'publish' === $post->post_status
+ && '' === $post->post_password
+ && in_array( $post->post_type, $post_types, true )
+ ) {
+ return $post;
+ }
+ }
+
+ return null;
+ };
+
+ // The pool selected by an earlier request, once it is more than 10 minutes old.
+ $stale_pool = null;
+
+ if ( $can_resample ) {
+ $cached = wp_cache_get( $cache_key, 'random-content' );
+
+ if ( is_array( $cached ) && isset( $cached['post_ids'], $cached['expires'] ) && is_array( $cached['post_ids'] ) ) {
+ if ( $cached['expires'] > time() ) {
+ // The archive had no eligible posts when the pool was selected.
+ if ( array() === $cached['post_ids'] ) {
+ return array();
+ }
+
+ $post = $select_post( $cached['post_ids'] );
+
+ if ( null !== $post ) {
+ return array( $post );
+ }
+ } else {
+ $stale_pool = $cached['post_ids'];
+ }
+ }
+ }
+
+ // Look up random post IDs until one of them is an eligible post.
+ for ( $attempt = 1; $attempt <= 3; $attempt++ ) {
+ if ( $attempt > 1 ) {
+ $request = str_replace(
+ $sample_clause,
+ _wp_random_content_sample_clause( wp_get_random_content_sample_ids() ),
+ $wpdb->remove_placeholder_escape( $query->request )
+ );
+ }
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The request is prepared by WP_Query::get_posts().
+ $post = $select_post( $wpdb->get_col( $request ) );
+
+ if ( null !== $post ) {
+ return array( $post );
+ }
+
+ if ( ! $can_resample ) {
+ return array();
+ }
+ }
+
+ /*
+ * Eligible posts are sparse among the post IDs, so select a pool of them.
+ *
+ * Only one request selects the pool at a time. While it does, other requests select
+ * from the previous pool, if there is one, rather than each reading every eligible
+ * post. wp_cache_add() only stores the lock if another request does not hold it.
+ */
+ $lock_key = $cache_key . ':lock';
+ $has_lock = wp_cache_add( $lock_key, 1, 'random-content', MINUTE_IN_SECONDS );
+
+ if ( ! $has_lock && null !== $stale_pool ) {
+ $post = $select_post( $stale_pool );
+
+ if ( null !== $post ) {
+ return array( $post );
+ }
+
+ if ( array() === $stale_pool ) {
+ return array();
+ }
+ }
+
+ $request = str_replace( $sample_clause, '1=1', $wpdb->remove_placeholder_escape( $query->request ) );
+
+ /*
+ * The post password is not indexed, so excluding password protected posts in SQL
+ * requires the database to read the full row of every eligible post. They are
+ * excluded when the post is selected from the pool instead, which allows the pool
+ * to be selected from an index.
+ */
+ $password_clause = " AND {$wpdb->posts}.post_password = ''";
+
+ if ( 1 === substr_count( $request, $password_clause ) ) {
+ $request = str_replace( $password_clause, '', $request );
+ }
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The request is prepared by WP_Query::get_posts().
+ $pool = array_map( 'intval', $wpdb->get_col( $request . ' ORDER BY RAND() LIMIT 1000' ) );
+ $post = $select_post( $pool );
+
+ /*
+ * A pool of fewer than 1,000 posts is the whole archive. If none of its posts are
+ * eligible, as they are all password protected, the archive has no eligible posts.
+ */
+ if ( null === $post && count( $pool ) < 1000 ) {
+ $pool = array();
+ }
+
+ /*
+ * The pool is used for 10 minutes. It is stored for an hour, so that requests can
+ * select from it while the next pool is being selected.
+ */
+ wp_cache_set(
+ $cache_key,
+ array(
+ 'post_ids' => $pool,
+ 'expires' => time() + 10 * MINUTE_IN_SECONDS,
+ ),
+ 'random-content',
+ HOUR_IN_SECONDS
+ );
+
+ if ( $has_lock ) {
+ wp_cache_delete( $lock_key, 'random-content' );
+ }
+
+ return null === $post ? array() : array( $post );
+}
+
/**
* Redirects random content requests to the randomly selected post.
*
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 8168317983..aff2ed86d7 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-beta5-65541';
+$wp_version = '7.2-beta5-65542';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.