Support » Developing with WordPress » Pagination when doing an AJAX query

  • Resolved bbtodd

    (@bbtodd)


    I set up a site specific plugin where I have the code to do an AJAX query. My problem is that when I try to add pagination to it the resulting pagination links look this:

    //https://mysite.org/wp-admin/admin-ajax.php?action=get_my_ajax_posts&tags=drama#038;tags=drama&paged=2

    that fails of course, normally pagination links look like this
    https://mysite.org/film-list/page/2/

    Is there some kind of trick to getting pagination in this situation? Or should I query for all posts and then figure out how to display 50 per page and do some kind of faux pagination with Javascript?

    Here is the code I use to get the pagination:

    $big = 999999999;
            $paginator = paginate_links( array(
                 'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                 'format' => '?paged=%#%',
                 'current' => max( 1, get_query_var('paged') ),
                 'total' => $search_query->max_num_pages,
                 'prev_text' => '«',
                 'next_text' => '»'
            ) );
    
                $result[0]["pagination"] = $paginator;
    • This topic was modified 2 months ago by bbtodd.
    • This topic was modified 2 months ago by bbtodd.
    • This topic was modified 2 months ago by bbtodd.
    • This topic was modified 2 months ago by bbtodd.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Well, admin-ajax.php/page/2/ isn’t going to work either 🙂 &paged=2 is fine. The data gets to the server, that’s all that really matters. From there, it’s up to your Ajax handler code to do what’s necessary to get the desired content. For example: it could get the passed value from $_GET['paged'] and use it as part of a get_posts() call to get the next 50 posts.

    It wouldn’t scale very well to get all posts and work out pagination through JavaScript. It may work for a few hundred posts, but not several thousand. It takes SQL to manage that many posts, you’ll have time and memory issues with JavaScript.

    `

    Thread Starter bbtodd

    (@bbtodd)

    OK, thanks for your reply, yes, faux Javascript pagination would get ugly, I can see what you’re saying to do here. So I guess paginate_links isn’t designed for this usage. I’ll have to get the page value from the URL and do another query each time. Shouldn’t be too hard to implement. Cheers.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.