Commit 8165dda612 for wordpress.org
commit 8165dda6122a8219a3e57b92cefdf3da26e810f4
Author: jorbin <jorbin@git.wordpress.org>
Date: Thu Nov 12 22:24:54 2026 +0000
Rewrite Rules: Give top rewrite rules priority over random content rules.
A post type whose archive is at the random base, by name, rewrite slug or `has_archive`, kept its rewrite rule but had the rule's query replaced by the random content query. As a result `/random/` either redirected to random content or returned a 404 instead of displaying the post type archive. The same applied to any rule a plugin added to the top using the same regex as a random content rule.
Random content rules using the same regex as a rule added to the top, including post type and taxonomy rules, are now omitted when the rewrite rules are generated.
Developed in: https://github.com/WordPress/wordpress-develop/pull/15959 and https://github.com/WordPress/wordpress-develop/pull/15838
Follow-up to r64402.
Props peterwilsoncc, jorbin, ssebuwufumoses, aryanjasala.
Fixes #66630. See #64498.
Built from https://develop.svn.wordpress.org/trunk@65441
git-svn-id: http://core.svn.wordpress.org/trunk@64580 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/class-wp-rewrite.php b/wp-includes/class-wp-rewrite.php
index 20967bafa0..d9f7a61f2e 100644
--- a/wp-includes/class-wp-rewrite.php
+++ b/wp-includes/class-wp-rewrite.php
@@ -1423,22 +1423,6 @@ class WP_Rewrite {
*/
$page_rewrite = apply_filters( 'page_rewrite_rules', $page_rewrite );
- // Random content rewrite rules.
- $random_rewrite = $this->random_rewrite_rules();
-
- /**
- * Filters rewrite rules used for random content redirects.
- *
- * Likely random content rules would include `/random/`, as well as
- * term, author and post type archives prefixed with the random base,
- * for example `/random/category/news/`.
- *
- * @since 7.2.0
- *
- * @param array<string, string> $random_rewrite Array of rewrite rules for random content redirects, keyed by their regex pattern.
- */
- $random_rewrite = apply_filters( 'random_rewrite_rules', $random_rewrite );
-
// Extra permastructs.
foreach ( $this->extra_permastructs as $permastructname => $struct ) {
if ( is_array( $struct ) ) {
@@ -1485,7 +1469,38 @@ class WP_Rewrite {
$this->extra_rules_top = array_merge( $this->extra_rules_top, $rules );
}
- // Put them together.
+ // Random content rewrite rules.
+ $random_rewrite = $this->random_rewrite_rules();
+
+ /*
+ * Rules added to the top, including those for post types and taxonomies, take
+ * precedence over random content rules using the same regex, for example a post
+ * type archive at `/random/`.
+ *
+ * Rules added to the top are merged ahead of the random content rules, so without
+ * this, the random content rule's query would replace the query of a rule using the
+ * same regex. Rules added to the top using a different regex for the same path,
+ * such as `^random/?$`, already take precedence as they are matched first.
+ */
+ $random_rewrite = array_diff_key( $random_rewrite, $this->extra_rules_top );
+
+ /**
+ * Filters rewrite rules used for random content redirects.
+ *
+ * Likely random content rules would include `/random/`, as well as
+ * term, author and post type archives prefixed with the random base,
+ * for example `/random/category/news/`.
+ *
+ * @since 7.2.0
+ *
+ * @param array<string, string> $random_rewrite Array of rewrite rules for random content redirects, keyed by their regex pattern.
+ */
+ $random_rewrite = apply_filters( 'random_rewrite_rules', $random_rewrite );
+
+ /*
+ * Put them together, with the random content rules ahead of the generic rules
+ * such as the page and post rules which would otherwise match them first.
+ */
if ( $this->use_verbose_page_rules ) {
$this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $sitemap_rewrite, $deprecated_files, $registration_pages, $random_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules );
} else {
@@ -1532,6 +1547,19 @@ class WP_Rewrite {
* the URL, see {@see WP_Rewrite::post_permalinks_share_random_base()}. The rules are flushed
* when such a page or post changes, see {@see wp_random_content_flush_rewrite_rules_for_post()}.
*
+ * Rules added to the top of the rewrite rules, including those for post types and taxonomies,
+ * take precedence over the random content rules. When the rewrite rules are generated, random
+ * content rules using the same regex as a rule added to the top are omitted, see
+ * {@see WP_Rewrite::rewrite_rules()}. For example, a post type archive at `/random/` replaces
+ * the `/random/` rule.
+ *
+ * If a post type or taxonomy uses the random base as its rewrite slug, its rules may match
+ * random archive URLs before the random content rules do. For example, the attachment rules
+ * of a post type with the rewrite slug `random` match `/random/category/news/`, and the rules
+ * of a hierarchical post type or taxonomy match every URL beginning with `/random/`. Such
+ * random archive URLs display the post type or taxonomy content instead. To use random
+ * archives on these sites, change the random base on the Permalinks screen.
+ *
* The `random` query variable can be added to any other archive or search URL, for example
* `/2026/?random`, to request a random post from that archive.
*
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 12377a2fd4..f13401f8df 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-beta5-65440';
+$wp_version = '7.2-beta5-65441';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.