Title: problem with get_posts() effecting the loop
Last modified: August 19, 2016

---

# problem with get_posts() effecting the loop

 *  [neonWired](https://wordpress.org/support/users/neonwired/)
 * (@neonwired)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/problem-with-get_posts-effecting-the-loop/)
 * I’m using get_posts() to create a secondary list of posts on a page rather than
   another loop as suggested in the docs, however it’s changing the existing loop
   from displaying the correct page contents to the first post that get_posts().
   Am i missing something here?
 * first list:
 *     ```
       $posts = get_posts();
       foreach( $posts as $post ) :	setup_postdata($post); ?>
       <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
       <p><?php the_date(); ?></p>
       <h3 class="entry-title"><?php the_title(); ?></h3>
       <div class="entry-content">
       <?php
       global $more;
       $more = 0;
       echo strip_tags(get_the_content());
       ?>
       </div>
       </div>
       <?php endforeach; ?>
       ```
   
 * main loop:
 *     ```
       <?php wp_reset_query(); ?>
       <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
       <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
       <?php if ( !is_front_page() ) { ?>
       <h1 class="entry-title"><?php the_title(); ?></h1>
       <?php } ?>
       <div class="entry-content">
       <?php the_content(); ?>
       </div>
       </div>
       <?php endwhile; ?>
       ```
   

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/problem-with-get_posts-effecting-the-loop/#post-1922620)
 * Try adding `<php wp_reset_query();?>` after the end of the secondary loop.
 *  Thread Starter [neonWired](https://wordpress.org/support/users/neonwired/)
 * (@neonwired)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/problem-with-get_posts-effecting-the-loop/#post-1922629)
 * Thanks for your suggestion, i found the bug in my code, it’s actually the use
   of the variable $posts in $posts = get_posts().
 * I assume that this means $posts is used by the loop but unless i declare it as
   a global shouldn’t it be local and not effect the loop? and shouldn’t wp_reset_query()
   reset it anyway?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/problem-with-get_posts-effecting-the-loop/#post-1922640)
 * Well spotted! $posts may be declared as global in a number of places in WP core.

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

The topic ‘problem with get_posts() effecting the loop’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [esmi](https://wordpress.org/support/users/esmi/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/problem-with-get_posts-effecting-the-loop/#post-1922640)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
