WordPress 7.2 adds a new feature that sends visitors to a random post on your site when they visit /random/. It’s turned on by default for every site, and there’s no setting to turn it off. If you’d rather not have it on your site, you need a line of code.
In this tutorial, we’ll take a look at what the feature does, how to turn it off, and how to keep it but limit which posts it picks from.
I’ve tested everything in this tutorial with the WordPress 7.2 beta, which is scheduled for release in early December.
What the random feature does
When someone visits example.com/random/, WordPress picks a random published post and redirects them to it. The same works for archives, so you can get a random post from a category, a tag or an author:
example.com/random/picks from all posts.example.com/random/category/expeditions/picks from the “Expeditions” category.example.com/random/tag/greenland/picks from the “Greenland” tag.example.com/random/author/fridtjof-nansen/picks from the posts of a single author.
It also works without pretty permalinks, by adding ?random to an archive or search URL, like example.com/?random. That’s how the feature started out: it’s based on Random Redirect, a plugin Matt Mullenweg released almost 20 years ago, which did the same thing with ?random.
Pages aren’t picked by default, and attachments never are. Custom post types get their own random URL if they have an archive, so if you run WooCommerce, /random/shop/ sends visitors to a random product. Only published posts are picked, so drafts, private and password-protected posts are skipped.
You can change the random part of the URL in “Settings” → “Permalinks”, in the new “Random base” field.
Why you might want to turn it off
For most sites, the random feature is harmless. The redirect tells search engines not to index it, so it won’t show up in search results as a page of its own.
There are a few cases where turning it off makes sense, though:
- You have a large site. Picking a random post means asking the database to shuffle all of your posts, which gets slower the more posts you have. And since every visit needs a new random pick, the redirect isn’t cached, so every request to
/random/goes all the way to the database. That makes it an easy target for bots and DDoS attacks: hitting/random/over and over could slow your site down, or take it offline. - You have old content you’d rather people didn’t stumble upon. If you too are embarrassed by what you thought made a good blog post ten years ago, you might not want
/random/sending visitors there. - You simply don’t want it. Which is a perfectly good reason too.
How to turn off random posts
There’s no interface setting for turning off the new random feature, but fortunately, doing so in code is very straightforward thanks to the new wp_enable_random_content_redirect filter. The best place for it is a must-use plugin, so it keeps working whichever theme you use, and can’t be deactivated by accident.
- Connect to your site with SFTP or your host’s file manager, and go to the
wp-contentfolder. - Create a folder called
mu-plugins, if there isn’t one there already. - In it, create a file called
disable-random-posts.php, and paste this code into it:
<?php/** * Plugin Name: Disable Random Posts */add_filter( 'wp_enable_random_content_redirect', '__return_false' );
- Go to “Settings” → “Permalinks” in the WordPress admin. You don’t need to change or save anything. Opening the page is enough.
That last step is easy to miss, but it matters. WordPress stores the rules for which URLs go where, and the rules for /random/ stay until they’re rebuilt. Until then, /random/ won’t redirect to a random post anymore, but it will show your latest posts. Opening the Permalinks page rebuilds the rules, and after that, /random/ shows a “Page not found” page like any other URL that doesn’t exist.
When it’s turned off, there are no /random/ URLs, the “Random base” field disappears from the Permalinks settings, and adding ?random to a URL does nothing.
How to exclude some content instead
I don’t mind having the new random feature on my own site, but there’s some content I’d rather it didn’t surface. Nothing embarrassing, mind you – I’m completely open about my Daring Fireball-inspired link blogging phase – but I switched from writing in Swedish to English back in 2014. Since most of my readers are international, I’ve left all of my Swedish posts out of the random picks with a date_query.
There are plenty of other ways to exclude content, though. Let’s go through a few of them.
Exclude a post type
Whether a post type is included is controlled by a new randomable setting for post types. By default, it’s on for post types that are publicly queryable and have an archive, which is why pages are left out. So if you have a custom post type with an archive, it’s included unless you exclude it.
If you register the post type yourself, in your theme or a plugin of your own, you can set randomable to false right in the registration:
register_post_type( 'book', array( 'label' => 'Books', 'public' => true, 'has_archive' => true, 'randomable' => false, ));
If you can’t change the registration, like for WooCommerce products, add this to your mu-plugin instead, and change product to the post type you want to exclude:
/** * Exclude a post type from random posts. * * @param array $args The post type arguments. * @param string $post_type The post type name. * @return array The modified post type arguments. */function my_exclude_post_type_from_random( $args, $post_type ) { if ( 'product' === $post_type ) { $args['randomable'] = false; } return $args;}add_filter( 'register_post_type_args', 'my_exclude_post_type_from_random', 10, 2 );
You can also add the filter to a regular plugin, or to your theme’s functions.php file. Just keep in mind that the exclusion stops working if you deactivate the plugin or switch themes.
Like when turning the feature off, open “Settings” → “Permalinks” afterwards to rebuild the rules.
Exclude a category
To keep posts in a category from being picked, add this to your mu-plugin. It uses wp_is_random_content_query() to target only the random requests. Replace 12 with the ID of your category, which you can find by opening the category in “Posts” → “Categories” and looking for the number after tag_ID= in the URL:
/** * Exclude a category from random posts. * * @param WP_Query $query The query object. */function my_exclude_category_from_random( $query ) { if ( function_exists( 'wp_is_random_content_query' ) && wp_is_random_content_query( $query ) ) { $query->set( 'category__not_in', array( 12 ) ); }}add_action( 'pre_get_posts', 'my_exclude_category_from_random' );
The function_exists() check makes sure the code doesn’t break your site if you add it before you’ve updated to WordPress 7.2.
With the category excluded, its own random URL, like /random/category/expeditions/, shows a “Page not found” page, since there are no posts left for it to pick from.
What if I already have a page at /random/?
Then your page wins. If a published page (or a post, depending on your permalink structure) already lives at /random/, WordPress leaves that URL alone, and the random feature only takes over the archive URLs, like /random/category/expeditions/.
If you want to use the random feature, but with a different URL, change the “Random base” in “Settings” → “Permalinks”. Set it to surprise, for example, and the URL becomes example.com/surprise/.
WordPress 7.2 is still in development, and some of the finer details above might change before the planned release in early December. I’ll update the tutorial once it’s out.


