Your Content Is Going To Get More Random! – Make WordPress Core


Maybe not your content per se, but the ability to discover it is with the introduction of the /random/ endpoint in WordPress 7.2. A request to example.com/random/ will lead end users to a randomly selected published post. The feature is on by default and is built on the existing rewrite, WP, and WP_Query APIs. If you have a legitimate reason to not support this on a site, or you simply hate fun, there is a filter to disable this entire feature.

The Random URLs

The random base is prefixed to existing archive paths. This default base is translatable and customizable with a new option on wp-admin/options-permalink.php. With the default base and pretty permalinks, the following rules are generated:

  • /random/ redirects to a random post.
  • /random/category/release/ redirects to a random post in the release category The ‘category’ taxonomy lets you group posts / content together that share a common bond. Categories are pre-defined and broad ranging.. Any publicly queryable taxonomy A taxonomy is a way to group things together. In WordPress, some common taxonomies are category, link, tag, or post format. https://codex.wordpress.org/Taxonomies#Default_Taxonomies. attached to an eligible post type gets a similar rule, for example /random/tag/dev-note/.
  • /random/author/jorbin/ redirects to a random post by that author.
  • /random/{post-type-slug}/ redirects to a random post of a custom post type WordPress can hold and display many different types of content. A single item of such a content is generally called a post, although post is also a specific post type. Custom Post Types gives your site the ability to have templated posts, to simplify the concept. that is randomable.

Randomable Post Types

A new randomable argument on register_post_type() controls eligibility. When not set, it defaults to true for post types that are publicly_queryable and have has_archive set. Among built-in types, post is randomable; page and attachment are not.

register_post_type(
        'musicals',
        array(
                'public'      => true,
                'has_archive' => true,
                'randomable'  => false, // Keep musicals out of /random/.
        )
);

Customizing So It’s a Little Less Random

The random post is chosen by the main query. WP::handle_random() runs the main query with three callbacks added for the duration of the request: wp_random_content_pre_get_posts() on pre_get_posts, wp_random_content_posts_orderby() on posts_orderby, and wp_random_content_post_limits() on post_limits. Together they restrict the query to eligible posts, order by RAND(), and limit the result to one row.

Because the selection goes through the main query, existing pre_get_posts callbacks that customize an archive also apply to its random redirect. To change the selection only for random requests, check the query with wp_is_random_content_query():

add_action(
        'pre_get_posts',
        static function ( WP_Query $query ) {
                if ( wp_is_random_content_query( $query ) ) {
                        $query->set( 'category__not_in', array( 525600 ) );
                }
        }
);

No Random, No Problem

If you prefer to disable random redirects on your site, you can use the wp_enable_random_content_redirect filter Filters are one of the two types of Hooks https://codex.wordpress.org/Plugin_API/Hooks. They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output.. A simple mu-plugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the WordPress.org Plugin Directory https://wordpress.org/plugins/ or can be cost-based plugin from a third-party. with add_filter( 'wp_enable_random_content_redirect', '__return_false' ); will mean no random rewrite rules are generated, the random base field is hidden on the permalinks screen, and the random query variable is ignored.

For more detail, see #64498 and changesets r64402, r64405, r65042, and r65250.

Props @peterwilsoncc, @annezazu, @jeffpaul for review.

#7-2, #dev-notes, #dev-notes-7-2

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 9   +   3   =