Title: Display random posts
Last modified: August 21, 2016

---

# Display random posts

 *  [Md Mosaober Hasan](https://wordpress.org/support/users/imon-hasan/)
 * (@imon-hasan)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/display-random-posts/)
 * `<?php query_posts('orderby=rand'); ?>`
 * when i add this code in my theme . i see every post atomically duplicated and
   show so many post .
 * pls help

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [Gregor](https://wordpress.org/support/users/vaderw/)
 * (@vaderw)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/display-random-posts/#post-4730389)
 * Hi imon,
 * How do you want to change what’s displayed?
 * Do you want a certain number of posts, posts only from one category, posts with
   a certain tag…?
 * Thanks
 * VW
 *  Thread Starter [Md Mosaober Hasan](https://wordpress.org/support/users/imon-hasan/)
 * (@imon-hasan)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/display-random-posts/#post-4730395)
 * no i am not changing any thing .
 *  when i add the code then i have too many post displayed- duplicated posts.
 * for example if i have 12 post . when i add this code it displayed 24 post .
 * thanks
 *  [Gregor](https://wordpress.org/support/users/vaderw/)
 * (@vaderw)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/display-random-posts/#post-4730401)
 * Understood, can you post the entire code of the file containing the query_posts
   as described above?
 *  Thread Starter [Md Mosaober Hasan](https://wordpress.org/support/users/imon-hasan/)
 * (@imon-hasan)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/display-random-posts/#post-4730404)
 * Yas !!
 *  Moderator [Jose Castaneda](https://wordpress.org/support/users/jcastaneda/)
 * (@jcastaneda)
 * THEME COFFEE MONKEY
 * [12 years, 2 months ago](https://wordpress.org/support/topic/display-random-posts/#post-4730407)
 * The question really is where are you putting it and as VaderW asked, what is 
   the code. If more than 10 lines use a gist, or pastebin. 🙂
 *  Thread Starter [Md Mosaober Hasan](https://wordpress.org/support/users/imon-hasan/)
 * (@imon-hasan)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/display-random-posts/#post-4730467)
 * this is my code
 *     ```
       <?php if(of_get_option('frontcat_checkbox') == "1"){ ?>
       <?php if(is_front_page()) {
       	 $args = array(
       				   'cat' => ''.$os_front = of_get_option('front_cat').'',
       				   'post_type' => 'post',
       				   'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
       				   'posts_per_page' => ''.$os_fonts = of_get_option('frontnum_select').'');
       	query_posts($args);
       } ?>
       <?php }?>
   
                          <?php query_posts('orderby=rand'); ?>
   
       				   <?php
   
       				    if(have_posts()): ?><?php while(have_posts()): ?><?php the_post(); ?>
                       <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
       ```
   
 *  [Gregor](https://wordpress.org/support/users/vaderw/)
 * (@vaderw)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/display-random-posts/#post-4730511)
 * Looks like when the if statements are true, you query_posts twice, duplicating
   the posts. To get around this move the orderby=rand into the $args array, adjust
   the second query_posts() to use this array in it’s query, and remove the first
   query_posts() completely.
 * So try…
 *     ```
       <?php if(of_get_option('frontcat_checkbox') == "1"){ ?>
       <?php if(is_front_page()) {
       // This sets the values of $args if the above both return true
       	 $args = array(
       				   'cat' => ''.$os_front = of_get_option('front_cat').'',
       				   'post_type' => 'post',
       				   'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
       				   'posts_per_page' => ''.$os_fonts = of_get_option('frontnum_select').'',
       				   'orderby' => 'rand');
       } ?>
       <?php }?>
   
        <?php
       // query_posts() occurs once, returning one set of posts matching the values of $args
       query_posts($args); ?>
   
       <?php
       if(have_posts()):
       // If we have the posts, display them ?>
       <?php while(have_posts()): ?>
       <?php the_post(); ?>
       <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
       ```
   
 *  Thread Starter [Md Mosaober Hasan](https://wordpress.org/support/users/imon-hasan/)
 * (@imon-hasan)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/display-random-posts/#post-4730543)
 * thanks
 * but its not working result is same

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Display random posts’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 3 participants
 * Last reply from: [Md Mosaober Hasan](https://wordpress.org/support/users/imon-hasan/)
 * Last activity: [12 years, 2 months ago](https://wordpress.org/support/topic/display-random-posts/#post-4730543)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
