Infinite next and previous post looping in WordPress

infinit next previous post looping

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
Minimalist WordPress Theme Development
WordPress Bug: Tags do not appear under WP_Query
  • Sagive Aug 17,2017 at 4:43 pm

    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.

  • SJ Jun 24,2017 at 12:49 am

    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!

  • Mike Chalmers Feb 4,2017 at 11:17 pm

    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 ); ?>”>

  • 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


    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,

  • 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.

    echo ‘Zurück‘;

    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‘;

  • 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


    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.


    • 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.

      • Sian Evans Aug 31,2017 at 2:02 am

        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 🙂

  • 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.

      • nic Dec 28,2016 at 8:37 pm

        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…”

Leave Your Comment

Your Comment*

Your Name*
Your Webpage