Title: Get random posts from current category.
Last modified: August 20, 2016

---

# Get random posts from current category.

 *  Resolved [gashface](https://wordpress.org/support/users/gashface/)
 * (@gashface)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/get-random-posts-from-current-category/)
 * Hi there, I am trying to get random posts from the current category of a post
   page. Like a sort of related posts using categories. I want these as thumbnails
   within a grid, which works fine… that is the counter part of my code (which needs
   to be in there)
 * Here is the start of the loop
 *     ```
       <?php
       $c = 1; //init counter
       $bpr = 3; //boxes per row
       foreach(get_the_category() as $category)
       {
         $cat = $category->cat_ID;
       }
       query_posts('cat=' . $cat . '&showposts=3&orderby=rand');
       if(have_posts()) :
       while(have_posts()) :
       the_post();
       ?>
       ```
   
 * At the moment it only shows one post and fails to retrieve anymore posts from
   within the category. any help?
 * thanks!

Viewing 1 replies (of 1 total)

 *  Thread Starter [gashface](https://wordpress.org/support/users/gashface/)
 * (@gashface)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/get-random-posts-from-current-category/#post-2282892)
 * Nevermind I have fixed it. for anybody else who is curious:
 *     ```
       <?php
       $c = 1; //init counter
       $bpr = 3; //boxes per row
       foreach(get_the_category() as $category)
       {
         $cat = $category->cat_ID;
       }
       query_posts('cat=.$cat.&showposts=3&orderby=rand');
       if(have_posts()) :
       while(have_posts()) :
       the_post();
       ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Get random posts from current category.’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [thumbs](https://wordpress.org/support/topic-tag/thumbs/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [gashface](https://wordpress.org/support/users/gashface/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/get-random-posts-from-current-category/#post-2282892)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
