I am currently developing an ajaxified theme for wordpress. I am listing the posts in a particular category, but the pagination returns -1 when clicked. Lets say I set showposts to 3 and a particular category has 2 posts, the link to previous posts does not show up as expected. Now if the category has 5 posts, the link to previous posts shows up, again as expected, but when I click on it, -1 is returned. Here is my code:

$category = get_category_by_slug($_POST['catname']);
$wp_query = new WP_Query();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$('.pagi a').click(function(e)
       var link = $(this).attr('href');
while ($wp_query->have_posts()) : $wp_query->the_post();
<div class="pagi"><?php next_posts_link('&laquo; Older Entries') ?></div>
<div class="pagi"><?php previous_posts_link('Newer Entries &raquo;') ?></div>

Also, the link of the previous post is http://domain.com/wp-admin/admin-ajax.php?paged=2 which I suspect might be the root of the problem.

Can anyone help please?


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

                    //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;
        echo false;
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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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