0

For some reason most of our blog pages show the same or very similar related posts Im using this code:

$tags = wp_get_post_tags($post->ID);

  if ($tags) {
  $tag_ids = array();

       foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
 shuffle($tag_ids);
  $args=array(
 'tag__in' => $tag_ids,
    'post__not_in' => array($post->ID),
  'posts_per_page'=>4, // Number of related posts to display.
  'caller_get_posts'=>1,
    'post_type' =>'therapyspark'
  );

  $my_query = new wp_query( $args );

  while( $my_query->have_posts() ) {

  $my_query->the_post();
  ?>

  <div class="relateddiv"><div class = "relatedimg">
    <a rel="external" class = "relatedlink" href="<?php the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
    </div><div class = "relatedtitle"><?php the_title(); ?></div>
    </a>
  </div>



  <?php}
  }
  $post = $orig_post;
  wp_reset_query();
  ?>

The shuffle doesnt seem to make a difference. What could I do to show different related posts

  • Are you using any WP_Cache, or other site caching method (W3 Total Cache, etc)? – Mike Baxter Sep 16 at 17:01
  • No. they arent all the same...Id just like have it suggest different related posts, like if you refreshed the page it would suggest others maybe – user718229 Sep 16 at 18:05
1

Randomizing your tags doesn't matter much to WP_Query. If you want random results, try adding 'orderby' => 'rand' to your $args. This should randomize the stories returned. If not, try applying shuffle() to $my_query before iterating through them. Some web hosts have reportedly disabled the 'orderby'=>'rand' method of querying posts. For these hosts shuffle() seems to still get the desired results.

NOTE: If you go with the shuffle() method, you probably want to replace the posts_per_page value with '-1' and add a loop counter to your output loop. Otherwise, you will always be shuffling the first four stories.

  • thanks it worked – user718229 Sep 17 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.