I'm currently building a theme for a client, and one of their requirements is endless scrolling on a "Charts" page. They want this to be paginated, and the endless scroll to load the next page below the first, displaying the most popular 40 tracks posted on the site.

To achieve this, I used a new WP_Query, which is running inside a custom page template. The endless scroll works by fetching the next page, and ending when it hits a 404. The problem is, extra pages continue to be loaded, even when there are no posts to display. The script never hits a 404 so it continues to load.

Here is my query:

$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query(); 

$count = 1; while ($wp_query->have_posts()) : $wp_query->the_post(); 

<div class="count"><?php echo $count; ?></div>

//loop here

<?php $count++; endwhile; ?>

<?php content_nav( 'nav-below' ); ?>

<?php else : ?>

<?php get_template_part( 'no-results' ); ?>

<?php endif; ?>

<?php $wp_query = null; $wp_query = $temp;?>

The exact same query works without any issue on the index page and archive pages. It only seems to happen when its in a page template.

I've been stumped by this for the last day or so, and I'm not sure as to why it's happening. Any ideas would be greatly appreciated :)

  • Have you searched this site for WP_Query, Pagination and Infinite Scroll? There are plenty of solutions, workarounds and sample code that deal with that...
    – brasofilo
    Feb 24 '13 at 5:13
  • I've spent the past day or so searching. Of course, I could have overlooked something, though everything I have tried hasn't worked so far.
    – Syndrohme
    Feb 24 '13 at 5:15
  • My suggestion is about this site, as I've seen many Q&A's dealing with it. Anyway, I'm not sure, and it was just an idea...
    – brasofilo
    Feb 24 '13 at 5:19
  • I appreciate your suggestion :) To clarify, I meant that I have indeed searched this site. I only asked as I'm having difficulty working this out, and everything I have found hasn't worked so far.
    – Syndrohme
    Feb 24 '13 at 5:21

The script never hits a 404 so it continues to load.

If I am looking at this correctly it won't ever hit a 404, and it shouldn't ever hit a 404.

You are loading a page template, the finding and loading of which is going to be done by the main query. That page is always going to be found-- ie, not 404.

Your secondary query-- the one that is loading the posts-- is inside the page template. It doesn't matter if there are posts or not, it won't trigger a 404. That part is long past by the time the secondary query runs.

I think this is fundamentally just a design problem.

I don't know how your AJAX works, but what you should be doing is checking for the presence of posts returned by the query, not for a 404 returned by the page. You stop when you get 0 posts. And you should really be using the AJAX API for this as well.

  • Ah, that makes a lot of sense. I'm very much a novice when it comes to javascript, so it's all handled by infinitescroll.js (not the wordpress plugin). Thanks for your answer, it cleared up a few questions :)
    – Syndrohme
    Feb 24 '13 at 14:18
  • You might want to look into some plugins. A WordPress plugin ought to (have a better chance of) handling this correctly.
    – s_ha_dum
    Feb 24 '13 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.