Code

Infinite next and previous post looping in WordPress

infinit next previous post looping
4

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!

Baki Goxhaj
I’m a web consultant specializing in WordPress. I love elegant solutions, quality code and a good laughter.
You may also like
That’s Awksome!
Why You Shouldn’t Use Tables for Web Layouts
18 Comments
  • Gil Oct 12,2016 at 7:49 pm

    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?

  • Camilo Buitrago Jun 16,2016 at 4:05 pm

    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.

  • Alex Jan 24,2016 at 4:42 pm

    Thanks a lot!
    Works perfectly

  • Danish Sep 17,2015 at 10:17 pm

    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 ?

  • Niall Mar 9,2015 at 11:11 am

    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

  • Rodrigo Nov 11,2014 at 2:48 pm

    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!

    • Baki Goxhaj Nov 20,2014 at 5:52 pm

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

  • Philip Sep 11,2014 at 10:42 am

    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();
    };
    ?>

  • Michael Apr 2,2014 at 5:23 pm

    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!

    • Baki Goxhaj Apr 2,2014 at 6:01 pm

      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.

    • Thad Jun 9,2014 at 7:26 pm

      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();

      • Baki Goxhaj Jun 12,2014 at 7:15 am

        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.

  • Jonathan Jan 30,2014 at 5:42 pm

    Just what I needed, Thanks!

  • William Jan 8,2014 at 10:27 am

    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

    • Baki Goxhaj Jan 12,2014 at 7:27 pm

      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.

  • David Nov 29,2013 at 9:29 am

    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!!

    • Jeff Jun 6,2014 at 11:03 am

      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.

Leave Your Comment

Your Comment*

Your Name*
Your Webpage