Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Infinite next and previous post looping in WordPress
* Infinite next and previous post looping in WordPress
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', '&larr; Previous Post');
} else {
$first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post();
echo '<a href="' . get_permalink() . '">&larr; Previous Post</a>';
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', 'Next Post &rarr;');
} else {
$last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
echo '<a href="' . get_permalink() . '">Next Post &rarr;</a>';

Its possible use a cpt and show only same taxonomies?

treetrum commented May 9, 2016

Awesome! Thanks for this!

I took inspiration from it and made my own version that returns the actual post object rather than a link.

If you're interested:

@danidanidantas it is possible, according to the documentation for get_adjacent_post

@danidanidantas I was searching for the same solution, you don't do it with get_adjacent_post, you add the following to the WP_Query string &post_type=name-of-post-type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment