Title: Randomize related posts
Last modified: August 19, 2016

---

# Randomize related posts

 *  [websiter](https://wordpress.org/support/users/websiter/)
 * (@websiter)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/randomize-related-posts-1/)
 * I have some code in place that gets the tags of the current posts and displays
   a list of related posts with the associated thumbnail. The problem is that the
   many of the same posts are showing up again and again on different posts because
   its only displaying the top 5 results returned. I need to randomize it a little
   so that the related posts are more varied, but still related by tag. Here is 
   the code:
 *     ```
       <!--BEGIN STORIES YOU'LL LOVE-->
       <?php
       				$tags = wp_get_post_tags($post->ID);
       				if ($tags) {
       					$tag_ids = array();
       					foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
   
       					$args=array(
       						'tag__in' => $tag_ids,
       						'post__not_in' => array($post->ID),
       						'showposts'=>5, // Number of related posts that will be shown.
       						'ignore_sticky_posts'=>1
       					);
       					$my_query = new wp_query($args);
   
       					if( $my_query->have_posts() ) {
       						echo '<div class="related-posts"><h2>Stories You\'ll Love</h2>';
       						while ($my_query->have_posts()) {
       							$my_query->the_post();
       						?>
       							<div class="related-post">
   
                                   <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
       							<?php show_thumb($width=110,$height=110,$crop='T',$quality=95,$blank=false); ?>
       							<?php the_title(); ?></a></div>
       						<?php
       						}
       						echo '<div style="clear:both"></div></div>';
       					}
       				};
       				wp_reset_query();
       ?><!--END STORIES YOU'LL LOVE-->
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [websiter](https://wordpress.org/support/users/websiter/)
 * (@websiter)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/randomize-related-posts-1/#post-1835370)
 * Sorry about the double post, that was an accident.

Viewing 1 replies (of 1 total)

The topic ‘Randomize related posts’ is closed to new replies.

## Tags

 * [tags](https://wordpress.org/support/topic-tag/tags/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [websiter](https://wordpress.org/support/users/websiter/)
 * Last activity: [15 years, 5 months ago](https://wordpress.org/support/topic/randomize-related-posts-1/#post-1835370)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
