How to Display Posts in Random Order and Retain Persistent Pagination in WordPress

Home » Snippets » How to Display Posts in Random Order and Retain Persistent Pagination in WordPress

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

Do you want to display your posts in a random order on your WordPress site while still retaining persistent pagination? By default, WordPress allows you to sort posts by date or other specific criteria, but randomizing the order can add a fresh element to your content. However, implementing this feature while keeping a consistent pagination structure can be a challenge. In this article, we will guide you through the process of displaying posts in a random order while ensuring that your pagination remains intact.

					// function to modify the main query
function wpturbo_modify_random_posts_order( $query ) {
    if ( $query->is_main_query() && $query->is_home() ) {
        // check if the custom query parameter is set
        if ( isset( $_GET['random'] ) && $_GET['random'] == 'true' ) {
            $query->set( 'orderby', 'rand' ); // set the orderby parameter to 'rand'
            $query->set( 'paged', get_query_var( 'paged' ) ); // retain persistent pagination
add_action( 'pre_get_posts', 'wpturbo_modify_random_posts_order' );

// function to add a random link to the post navigation
function wpturbo_add_random_link_to_navigation( $links ) {
    $random_link = '<a href="' . esc_url( add_query_arg( 'random', 'true' ) ) . '">Random</a>';
    $links['random'] = $random_link;
    return $links;
add_filter( 'next_posts_link_attributes', 'wpturbo_add_random_link_to_navigation' );
add_filter( 'previous_posts_link_attributes', 'wpturbo_add_random_link_to_navigation' );

The code snippet provided includes two functions that work together to display posts in a random order while retaining persistent pagination. This means that when users navigate through the pages of the post archive, the random order will be maintained.

The first function, wpturbo_modify_random_posts_order, is hooked into the pre_get_posts action. This function modifies the main query and checks if the current query is the main query and if it is the home page. Then, it checks if a custom query parameter called "random" is set and if its value is "true". If these conditions are met, the function modifies the query by setting the orderby parameter to ‘rand’, which tells WordPress to order the posts randomly.

Moreover, the function uses the get_query_var() function to retrieve the current page number and assigns it to the paged parameter of the modified query. This ensures that the pagination remains persistent and the user doesn’t get redirected to the first page when navigating through the posts.

The second function, wpturbo_add_random_link_to_navigation, is hooked into the next_posts_link_attributes and previous_posts_link_attributes filters. This function adds a random link to the post navigation, allowing users to easily switch between the regular ordered posts and the randomly ordered posts.

Within this function, we create a link using the esc_url() function to safely generate the URL with the custom query parameter "random" set to "true". Then, we assign this link to the "random" key of the $links array, which corresponds to the "Random" link in the post navigation.

By adding these two functions to your theme’s functions.php file or a custom plugin, you will be able to display posts in a random order while preserving persistent pagination. Users can then choose to view the posts in either the regular order or in a random order by simply clicking on the "Random" link in the post navigation.

Register an account to save your snippets or go Pro to get more features.