Select Page

If you don’t have any content on your homepage, you might want to redirect your website’s homepage to random blog posts on your blog. There’s a very simple way to do so. All you need to do is the following code snippet.

Copy & Paste this code snippet into your functions.php file:

// Check if this is the homepage.
if ( is_home() ) {
    query_posts('orderby=rand'); // Get posts by random order.
    while (have_posts()) {
       // If there are posts, then get the post.
        the_post();
        wp_redirect(get_permalink());
    }
}

The above code is simple. It first checks if we are on the homepage. And then it gets blog posts by random order. Then it redirects the user to the first post out of the random list of blog posts we queried.