• Resolved taker93

    (@taker93)


    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)
  • 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

    (@taker93)

    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)

    Try using that even after second loop.. No harm in trying!

    Thread Starter taker93

    (@taker93)

    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

    (@taker93)

    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
    [resolved] infinitescroll and wp_query is repeating posts

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

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