Skip to content

WIP: Use a 50 post pool for selecting random redirect. - #16044

Closed
peterwilsoncc wants to merge 3 commits into
WordPress:trunkfrom
peterwilsoncc:try/66654-random-with-50-post-pool
Closed

peterwilsoncc wants to merge 3 commits into
WordPress:trunkfrom
peterwilsoncc:try/66654-random-with-50-post-pool

Conversation

@peterwilsoncc

Copy link
Copy Markdown
Contributor

Vibe coded: not to be merged in its current state

Explores caching a pool of 50 randomly ordered posts for random content redirects, so that the database only orders every eligible post when the pool isn't cached.

  • The main query selects the IDs of 50 posts with ORDER BY RAND() LIMIT 0, 50 rather than a single post. Selecting 50 posts costs about the same as selecting one, as the database orders every eligible post either way.
  • The new wp_random_content_posts_pre_query() function on the posts_pre_query filter caches the pool of IDs in the post-queries group for an hour, salted with the posts last_changed value. Each request selects a post from the pool with wp_rand().
  • The pool is cached separately for each archive, and the cache is invalidated when posts change.
  • The pool is selected as an ids query, so only the selected post is loaded. The callback then sets the fields query variable to all, so the main query's post is set.
  • No changes to WP_Query.

Known issues

  • Up to 50 posts are served for up to an hour, so on large sites most posts can't be selected until the pool expires or posts change.
  • It doesn't help sites without a persistent object cache, which run the same query as trunk.
  • On sites where posts change often, the pool is rebuilt often and each rebuild costs the same as trunk.
  • Changing the fields query variable inside the filter is a workaround that relies on WP_Query reading it after posts_pre_query runs.

Trac ticket: https://core.trac.wordpress.org/ticket/66654

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Vibe coded POC. Pushed as a WIP draft to share with @aaronjorbin, not a merge candidate.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@github-actions

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 65542
GitHub commit: 3943b1d

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant