How to display Random Post in WordPress

Sometimes we may need to display the random posts in Worpdress. If you do not use any plugin you just need to touch in php file page where the posts are being displayed. This random post can be applied not only default posts, it can be applied to any custom-post types also. So, just look where to change the code to be add/change for displaying random posts.

First you need to ensure which post type you want to display randomly. Then you have to open that template and paste the below one line code before starting the loop:

 
query_posts(array('orderby' => 'rand');
//start your loop
if (have_posts()) :
while (have_posts()) : the_post();
//do your code inside loop
endwhile;
endif;

So you just need to change parameter value “orderby=>’rand’” which default set “orderby=>’date’”. Moreover if you use to call wp_query class then you need to set it inside your arguments:

 
$args= array('orderby' => 'rand', 'showpost' => 1, 'post_type' => 'post');
$wp_query = new WP_Query($args);
//start your loop
if (have_posts()) :
while (have_posts()) : the_post();
//do your code inside loop
endwhile;
endif;

In above example we mentioned our ‘post_type’ is “post” so if you wish to show your custom post randomly, just change the post type there. In addition, i also mentioned ‘showpost’ that is display only 1 post randomly in the loop. Maximum time people want to display single post or something link testimonial which will change each time page refresh. By this way we can make it. If still you have further issue to display post randomly just drop a comment, we will response within short time.

Thanks.

This post has already been read 5525 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

Share
Published by
Mehedi Hasan

Recent Posts

WP Share A Friend – A Way to Capture Lead

What is Lead Capture: In general sense lead capture designates a process or way of…

5 years ago

Unlimited Modal POPUPS on MouseClick

Introduction Unlimited Modal POPUPS on MouseClick: Everyone knows about popup during site visits and most…

5 years ago

WordPress Pregnancy Calculator Plugin

Brief about Pregnancy Calculator : This WordPress Pregnancy Calculator plugin allows users to display an…

5 years ago

Interactive Service Plugin with Hover Effects VC

Introduction About this Service Plugin: This Visual Composer Service Plugin works as an addon to…

6 years ago

WP Dynamic Query String

Introduction of Dynamic Query String : This plugin allows user to set dynamic query string…

6 years ago

How to make scroll PDF in iFrame for iPAD Safari

There is an occasional bug for iPhone and iPad even they are designed as well and…

7 years ago