On my homepage I display a loop of posts that I allow to be replaced by ajax search results and other custom loops. Those results are returned in a template with a custom wp_query. The issue is next_posts_link isn't returning the correct link for page 2 for the search loop. What I get is a link to http://mysite.com/wp-admin/admin-ajax.php?paged=2. Below is the entire setup I use to return the results. What do I need to change to get the right pagination?


    var search_val=$("#s").val();
    var $panel = $(this).closest(".panel-container").find(".panel");
            action : 'wpa56343_search',
            search_val : search_val
        function( response ) {
            $('#boxes').append( response ).masonry( 'reload' );


//function to pass query params

add_action('wp_ajax_wpa56343_search', 'wpa56343_search');
function wpa56343_search(){
 global $wp_query;
 $search = $_POST['search_val'];
 $args = array(
    's' => $search,
    'posts_per_page' => 10
 $wp_query = new WP_Query( $args );

 get_template_part( 'search-results' );


//Custom search query search-results.php

  while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>


<?php endwhile; ?>

<?php if ( $wp_query->max_num_pages > 1 ) : ?>

<div class="infinitescroll">
  <?php next_posts_link( __( 'Load more posts', $wp_query->max_num_pages ) ); ?>

<script type="text/javascript">
 // Infinite Scroll
 var href = 'first';
 $(document).ready(function() {
    navSelector : '.infinitescroll',
    nextSelector : '.infinitescroll a',
    itemSelector : '#boxes .box',
    loadingImg : '<?php echo get_bloginfo('stylesheet_directory') ?>/images/loading.gif',
    loadingText : 'Loading...',
    donetext : 'No more pages to load.',
    debug : false
}, function(arrayOfNewElems) {
    $('#boxes').masonry('appended', $(arrayOfNewElems));
    if (href != $('.infinitescroll a').attr('href'))
        href = $('.infinitescroll a').attr('href');
 <?php endif; ?>

next posts link is somewhat pointless since you're paginating with ajax, no? have a look at the code from the answer to your other question at how I paginate the query-

var postoffset = $('.post').length;

that counts how many posts are on the page and then I pass that as the offset. in your case it would be .box. when I put that in the query, I get back posts starting at that number:

$offset = $_POST['postoffset'];
$args = array(
    'offset' => $offset,
    'posts_per_page' => 10
$wp_query = new WP_Query( $args );

that way I can just use the same button to load more posts, it just repeatedly calls the same function, checking how many posts are on the page each time.

  • I just figured it needed to work properly in order for the infinite scroll to work. I loop the same way for the posts shown originally on my homepage that also uses infinite scroll and that works perfectly. It just stopped working when I replace the loop with the search results. – Pollux Khafra Jun 25 '12 at 22:57
  • ah, I see, you're using it with infinite scroll. I think you'll need to manually build a URL. – Milo Jun 25 '12 at 23:06
  • See I reload the entire script and everything inside the content. Infinite scroll works fine when loaded on the regular search page so it freaks me out that it doesn't work in this situation. How I can build a url manually that changes every time infinite scroll is triggered. – Pollux Khafra Jun 25 '12 at 23:17
  • @PolluxKhafra - maybe something like home_url().'/page/#/?s=term' where # is the page number and term is whatever was searched. infinite scroll has a navigation selector option, as long as the link you create matches that selector, the script should pick up the url and load the next page of posts. – Milo Jun 26 '12 at 23:54
  • well if you're only ever loading the first page of the search query via ajax, then your link would be to the second page, and at that point the normal next_posts_link will output the correct link. basically the infinite scroll script works the same way as option 1 that I mentioned in your other question. if you were to load the search query via that method, this problem would go away. – Milo Jun 27 '12 at 0:27

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.