Support » Plugin: WordPress Infinite Scroll - Ajax Load More » Multiple Loops and Duplicate Post

  • Resolved jchasef

    (@jchasef)


    Running into an issue. Everything works as expected, except when I use two loops on the home page with a do not duplicate array. All I want it to do, is to run the second loop, not the first, and to not duplicate any of the post ID’s stored in the array. With out the Ajax call, the logic works perfectly, but when I add the ajax call (click not scrolling) it repeats both loops, reseting the do not duplicate array, causing duplicate post. Please help.

    This is the loop below, nearly identical to they way WP suggest to use multiple loops one one page:

    <?php $my_query = new WP_Query( array( 
    	'category__and' => array(23,2) 
     ));
    while ( $my_query->have_posts() ) : $my_query->the_post();
    	$do_not_duplicate[] = $post->ID; ?>
    	<?php get_template_part( 'parts/loop', 'archive-grid' ); ?>
    <?php endwhile; ?>
    <?php query_posts( array( 
    	'post__not_in' => $do_not_duplicate,
    	'category__in' => array(2) 
    ));
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 
            <?php get_template_part( 'parts/loop', 'archive-grid' ); ?>
    <?php endwhile; endif; ?>

    Ajax Load More – Infinite Scroll works great. I’ve already recommended it to a few people, i just need to get past this one issue.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author dcooney

    (@dcooney)

    Hi @jchasef,
    In order to not duplicate posts you need to exclude the posts you are loading outside ajax.

    Like so:

    <?php 
       $post_ids = array('1, 1530', '1514', '1471'); // Array of posts to exclude
       if($post_ids){
       	$post__not_in = implode(",", $post_ids); 
       } 
       echo do_shortcode('[ajax_load_more post__not_in="'.$post__not_in.'"]'); 
    ?>

    Hope this helps.

    Hi @dcooney,

    This is a good start for me to have something to work with, but it looks like you are explicitly stating what ID’s go in the array, but what I need to use is the $do_not_duplicate array I already stated in the above loops, Which dynamically captures the post IDs.

    How do I replace the static array you have with the dynamic array/variable?

    `$do_not_duplicate

    Thank you,
    jChasef

    Plugin Author dcooney

    (@dcooney)

    I think you can just pass $do_not_duplicate to implode()

    <?php 
       if($do_not_duplicate){
       	$post__not_in = implode(",", $do_not_duplicate); 
       } 
       echo do_shortcode('[ajax_load_more post__not_in="'.$post__not_in.'"]'); 
    ?>

    Let me know.

    This worked perfectly! Exactly what I was trying to accomplish. Thank you!

    Plugin Author dcooney

    (@dcooney)

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Multiple Loops and Duplicate Post’ is closed to new replies.