Title: Transient Code affecting comments
Last modified: December 9, 2020

---

# Transient Code affecting comments

 *  Resolved [Karis](https://wordpress.org/support/users/askel45/)
 * (@askel45)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/transient-code-affecting-comments/)
 * I am using this code in single posts but it is affecting my comments (changing
   the comments of the post to the last post appearing in the sequence). Could someone
   guide me on how to stop this from happening?
 *     ```
       <?php
       // Get any existing copy of our transient data
       if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) {
           // It wasn't there, so regenerate the data and save the transient
           $randargs = array('orderby' => 'rand', 'numberposts' => 20);
           $special_query_results = get_posts($randargs);
           set_transient( 'special_query_results', $special_query_results, 60*60*12 );
       }
   
       // Use the data like you would have normally...
       $randomposts = get_transient( 'special_query_results' );
       $randkey = array_rand( $randomposts, 6 );
       $sixposts[0] = $randomposts[$randkey[0]];
       $sixposts[1] = $randomposts[$randkey[1]];
       $sixposts[2] = $randomposts[$randkey[2]];
       $sixposts[3] = $randomposts[$randkey[3]];
       $sixposts[4] = $randomposts[$randkey[4]];
       $sixposts[5] = $randomposts[$randkey[5]];
   
       global $post;
       foreach( $sixposts as $post ) :  setup_postdata($post); ?>
   
       <div class="gridcontainer">
       <div class="gridthumb"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
       <div class="gridcontent">
       <div class="gridtext"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
       </div>
       </div>
   
       <?php endforeach; ?>
       ```
   
 * Source of code: [https://www.wpbeginner.com/wp-tutorials/speed-up-your-wordpress-by-caching-custom-queries-using-transients-api/](https://www.wpbeginner.com/wp-tutorials/speed-up-your-wordpress-by-caching-custom-queries-using-transients-api/)

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

 *  Moderator [Tellyworth](https://wordpress.org/support/users/tellyworth/)
 * (@tellyworth)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/transient-code-affecting-comments/#post-13769015)
 *     ```
       global $post;
       foreach( $sixposts as $post ) :  setup_postdata($post);
       ```
   
 * I think this is the culprit. It’s altering the global `$post` object, which is
   the one displayed on the page.
 * Call [wp_reset_postdata()](https://developer.wordpress.org/reference/functions/wp_reset_postdata/)
   after your `endforeach`. You can read more about secondary loops here: [https://developer.wordpress.org/themes/basics/the-loop/#creating-secondary-queries-and-loops](https://developer.wordpress.org/themes/basics/the-loop/#creating-secondary-queries-and-loops)
    -  This reply was modified 5 years, 6 months ago by [Tellyworth](https://wordpress.org/support/users/tellyworth/).
 *  Thread Starter [Karis](https://wordpress.org/support/users/askel45/)
 * (@askel45)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/transient-code-affecting-comments/#post-13773885)
 * [@tellyworth](https://wordpress.org/support/users/tellyworth/) that solved the
   issue. Thanks a lot!

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

The topic ‘Transient Code affecting comments’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [Karis](https://wordpress.org/support/users/askel45/)
 * Last activity: [5 years, 6 months ago](https://wordpress.org/support/topic/transient-code-affecting-comments/#post-13773885)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
