next_posts_link not working for loop called with ajax

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?

//script

Read More
jQuery(document).ready(function($){
$('#searchsubmit').click(function(e){
    $('#boxes').empty();
    e.preventDefault();
    var search_val=$("#s").val();
    var $panel = $(this).closest(".panel-container").find(".panel");
    $.post(
        WPaAjax.ajaxurl,
        {
            action : 'wpa56343_search',
            search_val : search_val
        },
        function( response ) {
            $('#boxes').append( response ).masonry( 'reload' );
            $panel.hide("slow");
            $('.trigger').removeClass("active");
        }
    );
  });

});

//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' );

exit;
}

//Custom search query search-results.php

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

   <------content----->

<?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 ) ); ?>
</div>

<script type="text/javascript">
 // Infinite Scroll
 var href = 'first';
 $(document).ready(function() {
$('#boxes').infinitescroll({
    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');
    }
});
  });
 </script>
 <?php endif; ?>

Related posts

Leave a Reply

1 comment

  1. 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.