Title: Make related posts random
Last modified: August 30, 2016

---

# Make related posts random

 *  [Nikodemsky](https://wordpress.org/support/users/nikodemsky/)
 * (@nikodemsky)
 * [10 years, 5 months ago](https://wordpress.org/support/topic/make-related-posts-random/)
 * So I’m trying to make related posts on my website more random, atm. it shows 
   almost same posts on every other post. I’m pretty sure it’s about orderby arg
   but i’m not sure how to implement that.
 * Code i’m using for it:
 *     ```
       if(!function_exists('cherry_related_posts')){
       		function cherry_related_posts($args = array()){
       			global $post;
       			$default = array(
       				'post_type' => get_post_type($post),
       				'class' => 'related-posts',
       				'class_list' => 'related-posts_list',
       				'class_list_item' => 'related-posts_item',
       				'display_title' => true,
       				'display_link' => true,
       				'display_thumbnail' => true,
       				'width_thumbnail' => 250,
       				'height_thumbnail' => 150,
       				'before_title' => '<h3 class="related-posts_h">',
       				'after_title' => '</h3>',
       				'posts_count' => 14
       			);
       			extract(array_merge($default, $args));
   
       			$post_tags = wp_get_post_terms($post->ID, $post_type.'_tag', array("fields" => "slugs"));
       			$tags_type = $post_type=='post' ? 'tag' : $post_type.'_tag' ;
       			$suppress_filters = get_option('suppress_filters');// WPML filter
       			$blog_related = apply_filters( 'cherry_text_translate', of_get_option('blog_related'), 'blog_related' );
       			if ($post_tags && !is_wp_error($post_tags)) {
       				$args = array(
       					"$tags_type" => implode(',', $post_tags),
       					'post_status' => 'publish',
       					'posts_per_page' => $posts_count,
       					'ignore_sticky_posts' => 1,
       					'post__not_in' => array($post->ID),
       					'post_type' => $post_type,
       					'suppress_filters' => $suppress_filters
       					);
       				query_posts($args);
       				if ( have_posts() ) {
       					$output = '<div class="'.$class.'">';
       					$output .= $display_title ? $before_title.$blog_related.$after_title : '' ;
       					$output .= '<ul class="'.$class_list.' clearfix">';
       					while( have_posts() ) {
       						the_post();
       						$thumb   = has_post_thumbnail() ? get_post_thumbnail_id() : PARENT_URL.'/images/empty_thumb.gif';
       						$blank_img = stripos($thumb, 'empty_thumb.gif');
       						$img_url = $blank_img ? $thumb : wp_get_attachment_url( $thumb,'full');
       						$image   = $blank_img ? $thumb : aq_resize($img_url, $width_thumbnail, $height_thumbnail, true) or $img_url;
   
       						$output .= '<li class="'.$class_list_item.'">';
       						$output .= $display_thumbnail ? '<figure class="thumbnail featured-thumbnail"><a href="'.get_permalink().'" title="'.get_the_title().'"><img data-src="'.$image.'" alt="'.get_the_title().'" /></a></figure>': '' ;
       						$output .= $display_link ? '<a href="'.get_permalink().'" >'.get_the_title().'</a>': '' ;
       						$output .= '</li>';
       					}
       					$output .= '</ul></div>';
       					echo $output;
       				}
       				wp_reset_query();
       			}
       		}
       	}
       ```
   
 * i have tried changing line:
    `$post_tags = wp_get_post_terms($post->ID, $post_type.'
   _tag', array("fields" => "slugs"));` to `$post_tags = wp_get_post_terms($post-
   >ID, $post_type.'_tag', array("fields" => "slugs", "orderby" => "rand"));` but
   nothing changed.
 * Any help with that?

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

 *  [David Biňovec](https://wordpress.org/support/users/davidbinda/)
 * (@davidbinda)
 * [10 years, 5 months ago](https://wordpress.org/support/topic/make-related-posts-random/#post-6882635)
 * Hey there!
 * I’d say that the problem is in using [deprecated `{tax}` param of WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters).
 * Try to replace the `$tags_type" => implode(',', $post_tags),` by a proper `tax_query`:
 *     ```
       'tax_query' => array( array(
           'taxonomy' => $tags_type,
           'field' => 'slug',
           'terms' => $post_tags
       ) ),
       ```
   
 * That should do the trick!
 * I’d also suggested to not use `query_posts` but a [WP_Query instead](https://codex.wordpress.org/Class_Reference/WP_Query#Standard_Loop).
   In such case, replace
 *     ```
       query_posts($args);
       if ( have_posts() ) {
       ```
   
 * by
 *     ```
       $related_posts = new WP_Query( $args );
       if ( $related_posts->have_posts() ) {
       ```
   
 * and
 *     ```
       while( have_posts() ) {
       the_post();
       ```
   
 * by
 *     ```
       while( $related_posts->have_posts() ) {
       $related_posts->the_post();
       ```
   
 * and `wp_reset_query();` by `wp_reset_postdata();`
 *  Thread Starter [Nikodemsky](https://wordpress.org/support/users/nikodemsky/)
 * (@nikodemsky)
 * [10 years, 5 months ago](https://wordpress.org/support/topic/make-related-posts-random/#post-6882647)
 * Thank you very much!
 * While the script works, then I’m still getting same 3 related posts for every
   other post. There’s 14 posts for this post type(it’s custom post type btw.) and
   it still gets only same 3 every time on every posts, while it should randomize
   those related posts and it’s order.
 * Probably i’ve took wrong approach with that or i’m missing something, should 
   i still use orderby arg with that(somewhere) or should I do something else?
 *  [David Biňovec](https://wordpress.org/support/users/davidbinda/)
 * (@davidbinda)
 * [10 years, 5 months ago](https://wordpress.org/support/topic/make-related-posts-random/#post-6882678)
 * What really get’s posts and orders them is the `$related_posts = new WP_Query(
   $args );` in my example or the `query_posts($args);` in original example. So 
   if you want to randomise the order, use `'orderby' => 'rand'` key-value pair 
   in the `$args` array.
 * I’d say that the reason for getting stale data might be in the data itself. I
   mean all posts having all tags or a similar situation. If the `orderby` `rand`
   is used, then the posts won’t be ordered by default post date in descending order.

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

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

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 2 participants
 * Last reply from: [David Biňovec](https://wordpress.org/support/users/davidbinda/)
 * Last activity: [10 years, 5 months ago](https://wordpress.org/support/topic/make-related-posts-random/#post-6882678)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
