Skip to content

Query: Redirect searches for random content to the search results. - #15916

Open
aaronjorbin wants to merge 2 commits into
WordPress:trunkfrom
aaronjorbin:worktree-random-no-search
Open

aaronjorbin wants to merge 2 commits into
WordPress:trunkfrom
aaronjorbin:worktree-random-no-search

Conversation

@aaronjorbin

Copy link
Copy Markdown
Member

A search request that includes the random query variable redirected to a matching post or returned a 404, which revealed whether any post matched the search terms. Search requests are no longer random content requests. Instead they are redirected to the same search without the random query variable or the random base, before the main query is run.

The redirect retains the requested path and the other query string parameters, URL-encodes parameter names as well as values, and is skipped when the location would be the requested URL.

Requests for robots.txt, the favicon, sitemaps, feeds and trackbacks now ignore the random query variable, they are neither random content requests nor redirected.

Trac Ticket: https://core.trac.wordpress.org/ticket/66662

Use of AI Tools

Example disclosure:

AI assistance: Yes
Tool(s): Claude
Model(s): Fabel 5.2
Used for: Code Generation, Code Review, Risk Analysis


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.

aaronjorbin and others added 2 commits November 11, 2026 21:49
A search request that includes the `random` query variable redirected to a matching post or returned a 404, which revealed whether any post matched the search terms. Search requests are no longer random content requests. Instead they are redirected to the same search without the `random` query variable or the random base, before the main query is run.

The redirect retains the requested path and the other query string parameters, URL-encodes parameter names as well as values, and is skipped when the location would be the requested URL.

Requests for robots.txt, the favicon, sitemaps, feeds and trackbacks now ignore the `random` query variable, they are neither random content requests nor redirected.

See #66662.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@aaronjorbin
aaronjorbin requested a balanced review from Copilot November 12, 2026 03:57
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jorbin.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Redirect URL construction mishandles backslashes in request paths and parameter names, potentially changing search terms or collapsing distinct query parameters.

2 open issues
What changed in this PR

This PR separates search requests from WordPress random-content selection, redirecting searches to ordinary results before the main query runs.

Changes:

  • Adds early search redirects that remove random-content routing while retaining other request parameters.
  • Excludes searches and non-post endpoints from random-content handling.
  • Updates documentation and expands regression coverage for redirects, encoding, and request exclusions.
File Description
tests/​phpunit/​tests/​query/​randomContentRedirect.php Replaces random-search expectations and adds redirect regression tests.
src/​wp-includes/​query.php Updates documentation for random-content eligibility.
src/​wp-includes/​class-wp.php Adds early search redirects and query-parameter encoding.
src/​wp-includes/​class-wp-rewrite.php Documents search redirects instead of random-search selection.
src/​wp-includes/​class-wp-query.php Excludes searches and non-post endpoints from random-query classification.

Review effort: Balanced

$requested_path = (string) preg_replace_callback(
'|[^a-z0-9\-._~+%/]|i',
static fn ( array $matches ): string => rawurlencode( $matches[0] ),
$this->request
$encoded = array();

foreach ( $query_args as $name => $value ) {
$name = urlencode( wp_unslash( (string) $name ) );
@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.

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.

2 participants