Infinite next and previous post looping in WordPress

It has happened to me several times in my career as a WordPress freelancer to be in need of a infinite next and previous post looping in WordPress. And I have a solution for that.

By default WordPress will show no previous post link when you are viewing the first post and no next post link when you are viewing the last post. But there are cases when an apparently infinite looping is needed. To achieve that, what we practically need to do is when viewing the first post, the previous post link will jump you to the last post and when viewing the last post the next post link will jump to the first post.

The only issue that arises at this point is how do we know that we are are viewing the first post or the last post. WordPress comes to help at this point by providing the get_adjacent_post(). This function allows us to check if there is a next or previous post or not, and if there is not, we will provide one by querying the appropriate one with WP_Query().

Here is the finished code:

I hope this was helpful. Until next time, stay blessed!

0 responses to “Infinite next and previous post looping in WordPress”

  1. David Avatar
    David

    Nice. I needed an infinite next/previous post solution for my custom post types, and a small tweak of your code did the trick. Thanks very much!!

    1. Jeff Avatar
      Jeff

      Thanks Baki for posting your nice piece of code!
      But It doesn’t work for me with the Custom post type 🙁
      If i paste this code into my single post page, the last ‘next’ post link only refresh the same page and not jump to the first post, and same for the first post.
      what i’ve to do to make the code works with CPT please?

      Thanks you so much.

      1. nic Avatar
        nic

        You need to add your custom post type to the WP_Query. eg. “$last = new WP_Query(array(‘post_type’=> ‘your_custom_post_type’), ‘posts_per_page…”

  2. William Avatar
    William

    Hi,

    Thanks for posting this!
    A question though. This solution seems to loop all posts from all the categories. Any idea how to adjust this code so it only loops the posts from the specific category.php template? I have three different category templates.

    Regards,
    William

    1. Baki Goxhaj Avatar

      Hey William,

      Since the code uses custom queries, you have to adjust the query for that particular category. You can grab the id or slug of the category using get_queried_object() like this:

      $term_slug = get_queried_object()->slug;

      Hope it helps. Please let me know.

      1. Sian Evans Avatar
        Sian Evans

        Thanks so much for this code! I am having the same problem of wanting to exclude a category, and i’m not adept at coding enough to follow your instructions on how to do it. Could you please show me how the complete code would look like with an excluded category? Thanks in advance for your help 🙂

  3. Jonathan Avatar

    Just what I needed, Thanks!

    1. Baki Goxhaj Avatar

      Awesome! Thanks for letting me know.

  4. Michael Avatar
    Michael

    Sorry for such an amateur question, but I dropped your code into my php file and it doesn’t seem to do anything. I noticed in the block above there was no closing bracket, so i added ?> at the end, but that didn’t help.
    Possibly I’m putting this in the wrong place? I’m trying to get this to work on my single portfolio pages, and just dropped it under the tag in that php file. Please let me know, thanks!

    1. Baki Goxhaj Avatar

      I have not tested it in a Custom Post Type. It’s very possible you have to make some tweaks to the queries to make it work. Sorry.

    2. Thad Avatar

      Just modify the query, adding &post_type=your_post_type_name

      Ex. for a post type called “work”:
      $first = new WP_Query('posts_per_page=1&order=DESC&post_type=work'); $first->the_post();

      1. Baki Goxhaj Avatar

        That part is easy. The tricky part is that `next_post_link()` and `previous_post_link()` by default work only with the main loop, which only runs through posts.

  5. Philip Avatar
    Philip

    I used your script, but the next button don’t work at the first post. I added Thad’s solution to your script.

    the_post();
    echo ‘Zurück‘;
    wp_reset_query();
    };

    if( get_adjacent_post(false, ”, false) ) {
    next_post_link(‘%link’, ‘Nächste’);
    } else {
    $last = new WP_Query(‘posts_per_page=1&order=ASC&post_type=object’); $last->the_post();
    echo ‘Nächste‘;
    wp_reset_query();
    };
    ?>

  6. Rodrigo Avatar
    Rodrigo

    Nice, i dont know why the simple function of wordpress wasn’t working in my site, and with your code works just fine! Thanks a lot man!

    1. Baki Goxhaj Avatar

      Really happy to hear I could help. All the best.

  7. Niall Avatar

    Hi everyone,

    Thanks for the code its really helpful. There seems to be one issue though, the previous button works fine to loop back to the final post. However when on the final post and I click next instead of going back to the very first post. It just seems to reload the final one. I was wondering if anyone has come across this and if they discovered a solution.

    Thanks a million,
    Niall

  8. Danish Avatar
    Danish

    Hello,

    Can you please help me ?

    I am using Theie post slider, and I want to loop the post..

    How this can be done with that plugin ?

  9. Alex Avatar
    Alex

    Thanks a lot!
    Works perfectly

  10. Camilo Buitrago Avatar

    Thanks for your help. I applied this to one of my projects and it worked like a charm.

    The only change I had to do was to apply this code to a specific custom post type, so I just added &post_type=name-custom-type and it worked.

  11. Gil Avatar
    Gil

    Great Solution! thanks.
    Anyway I can get next and previous links loop for a certain Post Format only (e.g – standard and not images, quote etc etc..)?
    outside the loop in single.php?

  12. Mike Chalmers Avatar
    Mike Chalmers

    Hi is there any way to adapt this to work on an image? I’m using the following code to link the main post image to the next post and would like this to loop infinitely, as opposed to just the prev / next links:-

    <a href="ID ); ?>”>

  13. SJ Avatar
    SJ

    Did anyone post a fix for the “Next” button issue? In the ‘else’ bracket for the Next button, the loop does not increment to the next post, instead it retrieves the current post only.

    Thank for the code!

  14. Sagive Avatar

    This is a nice share but doesnt work properly.

    In the last and first links one of the links would point to the current post.

    You must use ‘post__not_in’ => get_the_ID();

    in order to avoid a loop back bug.