Ajax (jquery) wp_query pagination returns -1

First thing to do is check for any plugins that affect search results. The reason is that I had the ‘Search Everything’ plugin running. While it was meant to search for more results and highlight the search terms, it will change any instance of new WP_Query(); making it useless.

This is the callback function to load posts with ajax. The calling function works like any other client side request, echoing the results to an empty div.

function cbSearch()
{
    $counter = 1;
    $max = 4;
    $html = "";
    //Variables passed through $_POST
    $s = $_POST['src'];
    //the first time you call this function page number should be 1
    //Additional pages/functions/links are created with this code
    $page_num = $_POST['idx'];

    //Assuming the old $wp_query is basically null, 
    //there's no reason to copy previous parameters, just make a new one
    $search_query = array('s' => $s, 'posts_per_page' => $max, 'paged' => $page_num, 'post_type' => 'post', 'post_status' => 'publish');
    $search = new WP_Query($search_query);

    //use the object created, have_posts() alone will use global $wp_query
    if ($search->have_posts())
    {
            //The variables required to figure out next or previous page
            //Total number of post that match $s
        $total = $search->found_posts;
            //Post count for the page you're on
        $post_count = $search->post_count;
            //The maximum amount of pages to display all of the post results
        $page_count = ceil($search->max_num_pages);

        while ($search->have_posts())
        {
                    //Setup post data !important
            $search->the_post();

                    //Now your in the loop, use any loop functions you want
            $title = highlight(get_the_title(), $s);
        }
        $html = "<div id='news-page-nav'>";
            //Since we know what page we're on we know where to go
        $pre = $page_num - 1;
        $nex = $page_num + 1;
        if ($pre > 0)
        {
            $html .= "<span class="left fg-grey-light pointer" onclick='searchPosts(" . $pre . ");return false;' >P</span>";
        }
        if ($nex <= $page_count)
        {
            $html .= "<span class="right fg-grey-light pointer" onclick='searchPosts(" . $nex . ");return false;' >N</span>";
        }
        $html .= "</div>";
        echo $html;
    }
    else
    {
        echo false;
    }
    die();
}
function highlight($str, $mts)
{
    $keys = explode(" ", $mts);
    $title = preg_replace('/(' . implode('|', $keys) . ')/iu', '<span class="bg-highlight">\0</span>', $str);
    return $title;
}

It is impossible to use previous/next posts link with ajax because the links provided are urls which would load a new page. I had to remind myself that, with AJAX, the page does not reload to serve up new content. Where you have previous/next posts link I’ve created two ‘links’ that call the ajax function again but with a different page number. Now you can page search results without using WP’s default url parameters. Something to keep in mind is how this will affect search engine results.