0

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:

<?php
$category = get_category_by_slug($_POST['catname']);
$wp_query = new WP_Query();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query->query('cat='.$category->term_id.'&showposts=1&paged='.$paged);
?>
<script>
$('.pagi a').click(function(e)
   {
       e.preventDefault();
       var link = $(this).attr('href');
       $('#b_contentwrapper').load(link);
});
</script>
<?php
while ($wp_query->have_posts()) : $wp_query->the_post();
    get_the_title();
endwhile;
?>
<div class="pagi"><?php next_posts_link('&laquo; Older Entries') ?></div>
<div class="pagi"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
<?php
?>

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?

Thanks

1 Answer 1

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.

0

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.