Two custom loops, pagination is MESSED UP!

here's my problem: I'm on single.php and am trying to display at the bottom of the post a group of four related posts first, and then a list of all posts using infinite loading.

So I created two custom loops which return the correct posts, and the infinite loading also works. The only problem is, every time the page loads more posts for the bigger list, it pulls in the same four posts from the first group before outputting the next, correct posts.

Here's a schematization of what it's doing:

A B C D (First group)  ––––––– E F G H (Second group) A B C D I J K L A B C D M N O P A B C D etc. 

And here's the code for the two custom loops:

FIRST GROUP:

<?php   $Category = get_the_category(); $FirstCategory = $Category[0];  $args = array(   'category__in'  => $FirstCategory,   'posts_per_page' => 4 );  $MoreInCat = new WP_Query($args);   global $wp_query; $temp_query = $wp_query; $wp_query   = NULL; $wp_query   = $MoreInCat;  if ( $MoreInCat->have_posts() ) : while ( $MoreInCat->have_posts() ) :     $MoreInCat->the_post(); ?>  <?php get_template_part('loops/post-template'); ?>  <?php endwhile; ?>  <?php else: ?>  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>  <?php endif; wp_reset_postdata();  $wp_query = NULL; $wp_query = $temp_query; ?> 

SECOND GROUP:

<?php   $args = array(     'posts_per_page' => 4, );  $args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;     $AllPosts = new WP_Query($args);   $temp_query = $wp_query; $wp_query   = NULL; $wp_query   = $AllPosts;  if ( $AllPosts->have_posts() ) : while ( $AllPosts->have_posts() ) : $AllPosts->the_post(); ?>  <?php get_template_part('loops/post-template'); ?>  <?php endwhile; ?>  </div><!--.posts--> <p class="infinite-more-link-wrap"><?php next_posts_link( 'More...', $AllPosts->max_num_pages ); ?></p>   <?php else:  ?>  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> </div><!--.posts-->  <?php  endif; wp_reset_postdata();  $wp_query = NULL; $wp_query = $temp_query; ?> 

I've been looking everywhere for a solution but nothing seems to work...

Thanks in advance for your suggestions!

Replay

use wp_reset_query() after each while loop

Category: customization Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.266 (s). 12 q(s)