Support » Plugins » Hacks » Custom infinite scroll and archives/search

  • Resolved Adam


    I have a file (loop.php) which contains the loop (while (have_posts … endwhile)
    Next I have an index.php, archive.php and a search.php. All of which contain get_template_part(‘loop’).

    In my functions.php I add a hook called wp_ajax_infinitescroll which executes the following function:

    function MyInfiniteScroll(){
       $loop        = $_POST['loop'];
       $page           = $_POST['page'];
       query_posts(array('paged' => $page ));

    Then I have some JavaScript which does (I left out some code here to simplify):

        url: "http://xxxxxxxxx/wp-admin/admin-ajax.php",
        data: "action=infinitescroll&page="+ pagenr + '&loop=loop',
        success: function(html){

    My problem: on index.php this works perfectly, it shows all posts.
    On both archive.php and search.php the first “page” works also, but any subsequent “pages” load all the posts again and not those limited to the search or archive.
    I suppose somewhere along the line WP_Query is “reset”? So what am I doing wrong here/am I forgetting?

    PS: before someone tells me there is an infinite-scroll plugin, I know. However it is not usable in this particular case which is why I try to write my own.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I came to the conclusion I oversimplified my idea so I am completely rewriting this code from a different angle. Sorry for the void question, please ignore this thread.

    Hi Umbercode, did you ever find a solution to this?

    I have infinite scroll working on the search page, but loading page 2 just loads the default loop not those limited to the search.

    Hi John,

    Yes, I did manage to get it working…sort of.
    I spent too much time on the problem and in the end I got something that did what it had to do, but not without bugs. For the client it was good enough and I ran out of budget, so I left it at that.
    It has been a while and the project is archived, but I remember using this article as a startingpoint: (remember to also read the comments).
    Try it, if you still cannot get it to work I could go and find the code in my project.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom infinite scroll and archives/search’ is closed to new replies.