Load Random Post Using Ajax Call

Are you cluttered with posts? Are you having difficulties with viewing and managing all your old posts? Don’t worry we have just what you want.Our Random Post Generation will allow you to generate posts randomly. With Ajax we have been able to simplify random post generation with a special button function and we have also brought you this tutorial with sample codes.

The Required Pages are functions.php and randompost.js. Happy Coding!!!

First of all , Lets have a look for a simple query that allows you to display random posts.Here the magic is done by ‘orderby’=’rand’.


<?php $random_posts = new WP_Query(array( ‘post_type’ =’post’,’posts_per_page’=5,’orderby’ =’rand’ )); ?>


This will definitely load your posts randomly but the tedious part over here, you have to refresh your page everytime.

Dealing with ajax call for the first time?Here we have simplified the code,at the end of this tutorial you will be able to load your posts randomly with the button function using AJAX.

Lets have an example !

Start  by creating an input button on your html page.

<input id=”random_posts” type=”button” value=”Load random posts” /;>

Then add this code below the button tag. Id “ajax-posts” will trigger when you click button while reloading posts.

<div id=”ajax-posts” class=”layout”>


<?php $random_posts = new WP_Query ( array ( ‘post_type’ =’post’,’posts_per_page’ =2,’orderby’ =’rand’ )); if ( $random_posts->have_posts() ) : while ( $random_posts->have_posts() ) : $random_posts->the_post();

<?php the_title(); the_content(); endwhile; ?>
<?php wp_reset_postdata();?>

Add this below code on your functions.php


<?php function post_loadscript() { wp_register_script( ‘randomposts’, get_template_directory_uri(). ‘/js/randomposts.js’, ‘1.0’, ”, true); wp_localize_script( ‘randomposts’, ‘ajax_posts’,array( ‘ajaxurl’=> admin_url( ‘admin-ajax.php’ ), ));



<?php function random_post_ajax() { $random_posts = new WP_Query ( array ( ‘post_type’ =’post’,’posts_per_page’ =2,’orderby’ =’rand’ )); if ( $random_posts->have_posts() ) : while ( $random_posts->have_posts() ) : $random_posts->the_post();


<?php the_title(); the_content(); ?>

<?php endwhile; wp_reset_postdata(); endif; } add_action(‘wp_ajax_nopriv_random_post_ajax’, ‘random_post_ajax’); add_action(‘wp_ajax_random_post_ajax’, ‘random_post_ajax’); ?>


Finally its almost done .Now you have to simple copy paste this code on your js file.

[code lang=”js”]jQuery(document).ready(function($) {
function load_posts(){
var value = ‘&action=random_post_ajax’;
type: "POST",
dataType: "html",
url: ajax_posts.ajaxurl,
data: value,
success: function(data){
var $data = $(data);
} else{
error : function(jqXHR, textStatus, errorThrown) {
$loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
return false;
$("#random_posts").on("click",function(){ // When Button is Pressed.
$("#random_posts").attr("disabled",true); // Disable the button for temporary time.

This is all about loading random post using Ajax.Hope this tutorial helps you. 🙂

Leave a Reply