Commit 08d880be7e for wordpress.org
commit 08d880be7ef8f70c66d524ff6e3d4846c254e7cc
Author: jorbin <jorbin@git.wordpress.org>
Date: Thu Dec 3 19:04:08 2026 +0000
Rewrite Rules: Return a 404 for random requests with no randomable post types.
When a random rewrite rule was matched along with a `post_type` query var naming only post types that are not randomable, such as `/random/?post_type=attachment`, the request was served as a home-style listing of that post type with a 200 status. These requests now return a 404.
Developed in: https://github.com/WordPress/wordpress-develop/pull/16412
Follow-up to r64402.
Props therssoftware, jorbin, ravipatel, luciebesancon, suryakantupadhyay, peterwilsonccm ariel-b, peterwilsoncc.
Fixes #66897. See #64498.
Built from https://develop.svn.wordpress.org/trunk@65897
git-svn-id: http://core.svn.wordpress.org/trunk@64926 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/class-wp-rewrite.php b/wp-includes/class-wp-rewrite.php
index 7e1fe7cbe9..baf8f9863b 100644
--- a/wp-includes/class-wp-rewrite.php
+++ b/wp-includes/class-wp-rewrite.php
@@ -1541,6 +1541,8 @@ class WP_Rewrite {
* has one, otherwise its rewrite slug.
*
* Only post types registered as `randomable`, and taxonomies used by them, receive rules.
+ * A home or archive request matching a rule is a 404 when none of the queried post types
+ * are randomable, see {@see WP::handle_random()}.
*
* If a published page exists at the random base, the page takes precedence and the
* `/random/` rule is omitted. The same applies to a published post when posts share
diff --git a/wp-includes/class-wp.php b/wp-includes/class-wp.php
index 5c9b33a77a..bb8b08bd2f 100644
--- a/wp-includes/class-wp.php
+++ b/wp-includes/class-wp.php
@@ -733,6 +733,10 @@ class WP {
* the redirect. Otherwise the request continues with the results of the main
* query, which is not run again.
*
+ * A home or archive request matching a random content rewrite rule is also a 404 when
+ * none of the queried post types are randomable, for example
+ * `example.com/random/?post_type=attachment`. Singular requests are not affected.
+ *
* Search requests are never random content requests. A search request that includes
* the `random` query variable is redirected to the search results without it, see
* {@see WP::redirect_random_search()}.
@@ -792,6 +796,20 @@ class WP {
remove_filter( 'posts_pre_query', 'wp_random_content_posts_pre_query', 20 );
if ( ! wp_is_random_content_query( $wp_the_query ) ) {
+ /*
+ * A random content rewrite rule has no archive to display, so a home or archive
+ * request is a 404 when none of the queried post types are randomable.
+ */
+ if ( $wp_the_query->is_random() && array() === wp_get_random_content_post_types( $wp_the_query ) ) {
+ wp_parse_str( $this->matched_query, $matched_query_vars );
+
+ if ( isset( $matched_query_vars['random'] ) ) {
+ $wp_the_query->set_404();
+ status_header( 404 );
+ nocache_headers();
+ }
+ }
+
return true;
}
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 5b7860ae1d..da016368ac 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.3-alpha-65896';
+$wp_version = '7.3-alpha-65897';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.