Overview of the native /random/ Endpoint in WordPress 7.2
Starting in WordPress 7.2, native discovery mechanisms expand significantly with the introduction of the /random/ endpoint. Requesting example.com/random/ directs end users to a randomly selected published post on the site. Rather than relying on third-party redirection plugins or heavy custom template redirects, this feature is built directly into WordPress core using existing rewrite structures, the WP class lifecycle, and the WP_Query API.
By default, the random endpoint feature is enabled on fresh installations and existing upgrades. It hooks directly into the canonical permalink system, extending standard taxonomy, author, and custom post type archive rules. While designed to enhance content discovery and site engagement, the underlying mechanics interact directly with database ordering and query execution, making it essential for core developers and systems engineers to understand its exact execution model.
Endpoint URL Structure and Permalinks Integration
The random routing layer relies on prefixing a translatable and configurable random base to pre-existing archive path templates. Site administrators can customize or localize this base directly within the administration dashboard under Settings > Permalinks (wp-admin/options-permalink.php).
When pretty permalinks are enabled, WordPress automatically appends a series of rewrite rules to match random requests across core and custom archives:
- Global Random Post:
/random/resolves to a single random post across all eligible content types. - Taxonomy Archives:
/random/category/release/or/random/tag/dev-note/selects a random post assigned to that specific taxonomy term. Any publicly queryable taxonomy registered on an eligible post type inherits this URL structure automatically. - Author Archives:
/random/author/jorbin/redirects the browser to a random post published by the targeted author. - Custom Post Type Archives:
/random/{post-type-slug}/evaluates against a specific custom post type archive (e.g.,/random/portfolio/).
When matched, WordPress processes the request through main query parsing and executes a HTTP 302 temporary redirect leading the user to the canonical URL of the randomly selected post.
Post Type Eligibility and the ‘randomable’ Registration Argument
Not all post types registered within a WordPress environment are automatically exposed to random selection routes. Core controls eligibility using a dedicated registration argument named randomable within register_post_type().
When randomable is not explicitly set in the post type argument array, WordPress calculates its default state based on existing post type properties. A post type is considered randomable => true by default only if both publicly_queryable and has_archive evaluate to true. Among built-in content types:
postis randomable.pageis not randomable.attachmentis not randomable.
Developers registering custom post types can explicitly define whether or not a post type should be eligible for /random/ endpoints. For example, if a site contains a public musicals post type with an archive, but business requirements mandate that individual items must not be selected at random, randomable can be toggled to false:
register_post_type( 'musicals', array(
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'randomable' => false, // Excludes this post type from /random/ routes
'labels' => array(
'name' => __( 'Musicals', 'textdomain' ),
),
) );
Internal Query Lifecycle: WP::handle_random() and Core Callbacks
The processing of a random request occurs within the core routing phase. When rewrite rules detect a random route, the WP::handle_random() method executes and modifies the main query parameters dynamically.
To assemble and isolate the query without permanently polluting global query states, WP::handle_random() attaches three temporary core callbacks to the main query hooks for the exact duration of the request lifecycle:
wp_random_content_pre_get_posts()hooked topre_get_posts: Restricts the main query parameters to eligible, published posts matching any active taxonomy or author context.wp_random_content_posts_orderby()hooked toposts_orderby: Forces the database ordering clause toRAND().wp_random_content_post_limits()hooked topost_limits: Enforces a SQL limit ofLIMIT 1to retrieve exactly one database record.
Because selection occurs inside the standard main query pass, any established hooks—such as secondary pre_get_posts callbacks filtering custom taxonomy terms or post statuses—will continue to execute. Once the post ID is retrieved, WordPress resolves its permalink and issues the redirect.
Filtering and Customizing Random Query Logic
Because the random endpoint operates directly within WP_Query, site developers can tailor the query behavior dynamically. To distinguish standard archive or search queries from dedicated random selection queries, core provides the conditional helper function wp_is_random_content_query().
This function accepts a WP_Query instance and returns true if the query is currently processing a random content redirect request. For instance, if you want to prevent posts from specific internal categories from appearing in random results, you can check the query object inside a pre_get_posts filter:
add_action( 'pre_get_posts', static function ( WP_Query $query ) {
if ( wp_is_random_content_query( $query ) ) {
// Exclude a specific restricted category ID (e.g., 525600)
$query->set( 'category__not_in', array( 525600 ) );
}
} );
Using wp_is_random_content_query() ensures that exclusions or tax_query modifications apply strictly to the random endpoint without unexpectedly altering standard category archives, taxonomy views, or main RSS feeds.
Performance Implications and Scalability Limitations
While the random endpoint provides an engaging feature out of the box, system architects and database administrators should carefully consider its performance characteristics on high-traffic or large-scale sites.
At the database layer, selecting a random row via ORDER BY RAND() LIMIT 1 requires MySQL/MariaDB to build a temporary table, assign a random index value to every matching row, and perform a full filesort operation. On small sites with a few hundred posts, this operation completes in milliseconds. However, on databases containing hundreds of thousands of posts, executing ORDER BY RAND() on unindexed main queries can lead to disk I/O bottlenecks and elevated CPU usage.
Key mitigation strategies for enterprise applications include:
- Object & Page Caching Considerations: Because HTTP redirects from
/random/should vary on every request, edge caches (such as Varnish, Nginx fastcgi_cache, or Cloudflare) must be configured to bypass or carefully handle caching headers for paths matching the/random/base. - Query Scope Reduction: Combining random requests with contextual sub-paths (like
/random/category/specific-slug/) helps narrow the filtered post set before theRAND()ordering is evaluated by the database engine.
Completely Disabling Random Redirects
For enterprise environments, decoupled headless architecture setups, or site owners who wish to opt out of random content generation entirely, core offers a global disable hook.
To turn off the feature entirely across the site, return false on the wp_enable_random_content_redirect filter. This can be implemented in a custom plugin or Must-Use (MU) plugin:
<?php
/**
* Plugin Name: Disable Random Redirects
* Description: Completely disables the native WordPress random content endpoint.
*/
add_filter( 'wp_enable_random_content_redirect', '__return_false' );
When this filter returns false, WordPress executes the following clean-up steps automatically:
- No random-related rewrite rules are generated or flushed into the rewrite array.
- The random base setting field is completely hidden from the Settings > Permalinks dashboard.
- The internal
randomquery variable is ignored, preventing arbitrary request execution.
For further technical context and historical changesets behind this implementation, refer to Trac ticket #64498 and changesets r64402, r64405, r65042, and r65250.
Frequently asked questions
What is the primary function of the /random/ endpoint in WordPress 7.2?
The /random/ endpoint automatically routes incoming HTTP requests to a randomly selected published post matching the specified context (such as global posts, custom post types, categories, tags, or author archives).
How do I exclude a custom post type from being accessible via /random/?
Set 'randomable' => false inside the arguments array when registering the post type using register_post_type().
How can I check if a query is a random endpoint query in code?
Pass the $query object to the conditional helper function wp_is_random_content_query( $query ). It returns true if the query is currently handling a random content redirect.
How do I completely disable the /random/ feature across my site?
Add add_filter( 'wp_enable_random_content_redirect', '__return_false' ); in a custom plugin or mu-plugin. This removes rewrite rules, hides admin settings, and ignores random query variables.
Primary reference: Review the original announcement for exact release details. This article is an independent explanation and does not reproduce the source text.
