How To Display Random Post with Thumbnail in WordPress Without A Plugin

Earlier this week, I had a chance to add useful knowledge in WordPress. It was about displaying Random Post without Plug in. This information came from a friend who suggested adding a random post on the sidebar of my website.

We thought this would be a very easy task. With just a sprinkle of PHP magic and a little bit of Google Search, we already have our own Random Post Display on our Sidebar. But we’re wrong. The problem with the Random Post PHP code we found was it ruins the website’s Comment system. To elaborate what happened: When a user reads one of our blog post (e.g. Blog post # 1) and comments on it, his comments will be displayed to blog post # 2. Totally weird! We decided to remove temporarily the Random Post Display and try to work on the broken PHP Code.

After few hours fixing the code, we arrived to the new and improved Random Post Display without installing a WordPress Plug in.

The Code

Copy and paste the code below in your WordPress Theme File. You need to paste the codes where you want to see the Random post in action.

    5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1); $rand_posts = get_posts( $args ); foreach( $rand_posts as $post ) : ?>
  • Numberpost – how many post you would like to be displayed?
  • Orderby – since this is a Random Post, we will assign “rand” to randomly select from the list of our blog post.
  • Post_status – selects only the blog post which is on Publish status.
  • Offset – number of post to displace or pass over.

Your random post display will appear with no formatting or probably depends on your WordPress Theme’s default CSS formatting. You can modify the look and feel of your Random Post display by writing new CSS styles.

Adding the Post Thumbnail in your Random Post Display

To be more appealing to the audience, we decided that our random post display will display its featured image. At this point, we need to add another few set of codes in our Function.php file.

add_image_size( 'custom-post-thumbnail', 80, 65  ); // assign any class name, width, height

This will enable you to assign the featured image in every post you want to publish. In this case, we need to integrate the featured image in the Random post display. What we need to do is modify the codes above and add this code:

Final Random Post with Post Thumbnail Display

We modified the codes to be more suitable for our needs? Feel free to make some changes and apply your own css styles.

    7, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1); $rand_posts = get_posts( $args ); foreach( $rand_posts as $post ) : ?>

Final Words.

While this is a solution that will favor many, many would still argue that it is easier to use a WordPress plug in. I agree on that. But in our case, we’re only beginners on WordPress world and we really want to expand our knowledge in PHP so instead of using a plug in we decided to make our own.

So what do you think of our WordPress Random Post with Thumbnail Display?

Filed Under

Ben Daggers

is the creator of He graduated from Adamson University (Philippines) with a Degree in Bachelor of Science in Accountancy dating back 2009 and earning his MBA in Polytechnic University of the Philippines. He is currently employed as a General Accountant in a well known Global Business Process Outsourcing. He enjoys learning new things. He consumes his spare time in front of his Computer with Adobe Photoshop and Dreamweaver.

Leave a Reply

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

One Response to How To Display Random Post with Thumbnail in WordPress Without A Plugin

  1. Bex says:

    Thank you for sharing Ben! The live screenshots of my site’s WordPress template showed a random post thumbnail display on the sidebar but I have been unable to access it with the stock features. None of the plugins I have messed with look quite right so I will attempt your method. :-)