Ever though of displaying Random posts in WordPress ? Then this tutorial on how to display random posts in WordPress without a plugin is for you. using too many plugins may effect your blog loading time and it is recommended to go with code, wherever simple function is required.

This php code can be used inside text widget, read How to Execute php in WordPress Text Widget for more info.
Display Random Posts in WordPress
Use this code in any part of your theme to display random posts.
<div> <h2>Random Posts</h2> <ul> <?php $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1); $rand_posts = get_posts( $args ); foreach( $rand_posts as $post ) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </div>
Functionality of this Code:
- It will simply display a list of random posts in your blog.
- Code used here displays 5 random posts and that value can be changed.
- Change the value of
'numberposts' => 5to any positve integer.
Read This : How to Disable or Limit Post Revisions in WordPress
Display Random Posts with Excerpt in WordPress
Use this code in any part of your theme to display random posts.
<div> <h2>Random Posts</h2> <ul> <?php $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1); $rand_posts = get_posts( $args ); foreach( $rand_posts as $post ) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><p><?php the_excerpt(); ?></p></li> <?php endforeach; ?> </ul> </div>
Functionality of this Code:
- It simply displays a list of random posts along with post-excerpts.
- Code used here displays 5 random posts and that value can be changed.
- Change the value of
'numberposts' => 5to any positve integer.
Also Check : How to add 3 column footer to WordPress theme
Styling Random Posts in WordPress with CSS
After adding the random posts code, its time to style them. Add the below code to style.css file in your WordPress theme.
.random-posts {
margin: 10px 0;
}
.random-posts p{
font-size:13px;
}
.random-posts h2{
font-size: 16px;
font-weight: bold;
}
.random posts ul {
list-style: square;
margin: 10px 0;
}
.random posts ul li {
padding: 3px 0;
border-bottom: 1px dashed #ccc;
}
.random posts ul li a {
font-size: 14px;
text-decoration: none;
}
You have successfully added random posts in WordPress blog without a plugin, hope you like this tutorial and if you got any query on this, please do drop your valuable comments.


How to Remove Comment Author Website Link in WordPress
How to add 3 column footer to WordPress theme
How to Leverage Browser Caching on WordPress Blog
How to Disable or Limit Post Revisions in WordPress
How to Display WordPress Categories in two Columns
How to Login to WordPress using Email Address
Hello! Actually, I’m a bit confused. When you say ‘Display Random Posts’, do you mean on the homepage? Or is it on the sidebar? Please advice FOR THE THEME I AM USING. Please check my site and advice me on what should I do? Because I have the ‘Slider on the top’ where I can choose the category I want to display posts from, hence I’ve made a category called ‘Featured’ and have just put 4 posts in it. But then, do you think that showing those posts even on the home page looks good? Or should those posts on the homepage be different than those in the featured slider?
Thanks!
Using this code Random Posts can be displayed on homepage, single post page or in sidebar. And, these Random posts are not based on “categories”.
Awesome Work!
First Method is too Simple and I have done on my Blog.
Nice theme too.
Rehards,
Sohil Memon.
Awesome tutorial.Let me try it with my blog also.Thanks