Title: WP_Query returns post multiple times
Last modified: August 30, 2016

---

# WP_Query returns post multiple times

 *  Resolved [taker93](https://wordpress.org/support/users/taker93/)
 * (@taker93)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/wp_query-returns-post-multiple-times/)
 * Hi,
 * I googled for that problem and already used the search function, but I couldn’t
   find anyone with the same problem.
 * On my index.php i’m using two loops.
 * First:
 *     ```
       <?php
       				$not_again = array();
       				$args = array( 'numberposts' => 4, 'meta_key' => 'ratings_average', 'orderby' => 'meta_value_num', 'order' => 'DESC','category_name' => 'showcase', );
       				$postslist = get_posts( $args );
       				foreach ( $postslist as $post ) :
       				setup_postdata( $post );
       				$not_again[]= $post->ID;
       			?>
       ```
   
 * Second:
 *     ```
       <?php
       		$args = array('posts_per_page' => -1, 'post__not_in' => $not_again, 'post_type' => 'post', 'orderby' => 'date',
       					'order'   => 'DESC','category_name' => 'showcase');
       		$my_query = new WP_Query($args);
       		if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
       ```
   
 * I’m saving the posts shown in the first loop IDs in the $not_again Array. Then
   i’m using that array with the post__not_in Option.
 * My problem: (example)
    Loop 1 returns: 1 2 3 4 Loop 2 without category_name returns:
   5 6 7 8 Loop 2 with category_name returns: 5 6 7 8 5 6 7 8
 * Anyone ever expierenced that? Any ideas how to solve it?
 * Thank you in advance!

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

 *  [Tejas -The WP Chef](https://wordpress.org/support/users/tejasbusiness/)
 * (@tejasbusiness)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/wp_query-returns-post-multiple-times/#post-6524446)
 * Hi Taker93
 * As rule of thumb after every custom query, we need to use “wp_reset_postdata();”
   in order to reset to default otherwise there is every possibility of false output.
 * So, try and see if that solves your issue.
 * Hope this might help you
 * Cheers 🙂
 * Tejas
 *  Thread Starter [taker93](https://wordpress.org/support/users/taker93/)
 * (@taker93)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/wp_query-returns-post-multiple-times/#post-6524448)
 * Hi Tejas,
 * thank you for the reply.
 * I’m already using
 *     ```
       <?php
       			endforeach;
       			wp_reset_postdata();
       		?>
       ```
   
 * at the end of the first loop.
 * I don’t know if i can use it at the end of the second because the theme uses 
   Masonry.js . (somekind of infinite scroll stuff)
 *  [Tejas -The WP Chef](https://wordpress.org/support/users/tejasbusiness/)
 * (@tejasbusiness)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/wp_query-returns-post-multiple-times/#post-6524450)
 * Try using that even after second loop.. No harm in trying!
 *  Thread Starter [taker93](https://wordpress.org/support/users/taker93/)
 * (@taker93)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/wp_query-returns-post-multiple-times/#post-6524454)
 * Well…the posts are somekind of unique now. BUT: Now I see the navigation buttons,
   which should not be displayed…
 * Thank you so far.
 * Edit:
    Result without categories is still: Posts are not unique.
 * Edit 2:
    I tried:
 *     ```
       <?php $not_again = array();
       				$args = array( 'numberposts' => 4, 'meta_key' => 'ratings_average', 'orderby' => 'meta_value_num', 'order' => 'DESC','category_name' => 'showcase', );
       				$postslist = get_posts( $args );
       				foreach ( $postslist as $post ) :
       				$not_again[] = $post->ID;
       				setup_postdata( $post );
       			?>
       ```
   
 * …
 *     ```
       <?php
       			endforeach;
       			wp_reset_postdata();
       		?>
       ```
   
 *     ```
       <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       			<?php if ( in_array(get_the_ID(), $not_again) ) continue; ?>
       ```
   
 * …
 *     ```
       <?php endwhile;
       		else :
       			echo prpin_get_no_result();
       		endif;
       		wp_reset_postdata();
        		?>
       ```
   
 * Result is: Posts are unique. But “previous”-Button is being shown.
 *  Thread Starter [taker93](https://wordpress.org/support/users/taker93/)
 * (@taker93)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/wp_query-returns-post-multiple-times/#post-6524751)
 * Found out, that the problem was caused by wrong pagination. I disabled the inifinite-
   scroll script and changed the way of querying.
 * These posts were very helpful:
    [[resolved] Pagination with wp_query and custom post type](https://wordpress.org/support/topic/pagination-with-wp_query-and-custom-post-type?replies=3)
   [[resolved] infinitescroll and wp_query is repeating posts](https://wordpress.org/support/topic/infinitescroll-and-wp_query-is-repeating-posts?replies=4)

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

The topic ‘WP_Query returns post multiple times’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [taker93](https://wordpress.org/support/users/taker93/)
 * Last activity: [10 years, 8 months ago](https://wordpress.org/support/topic/wp_query-returns-post-multiple-times/#post-6524751)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
