Skip to content

Instantly share code, notes, and snippets.

@treetrum
Last active August 25, 2021 10:30
Show Gist options
  • Save treetrum/3a843c961aed69c732f0a61da854b16d to your computer and use it in GitHub Desktop.
Save treetrum/3a843c961aed69c732f0a61da854b16d to your computer and use it in GitHub Desktop.
Get Next or Previous Post (Looped). Will continue working once end of posts reached.
<?php
/**
* Gets the next post in the current post type, even if is last post
* Inspired from https://gist.github.com/banago/5603826
*
* @method get_next_post_looped
* @return WordPress Post Object
*/
function get_next_post_looped() {
$post_type = get_post_type();
if ( get_adjacent_post( false, '', false) ) {
return get_next_post();
} else {
$loop = new WP_Query( 'posts_per_page=1&order=ASC&post_type=' . $post_type );
$loop->the_post();
$postToReturn = get_post();
wp_reset_query();
return $postToReturn;
}
}
/**
* Gets the previous post in the current post type, even if is last post
* Inspired from https://gist.github.com/banago/5603826
*
* @method get_previous_post_looped
* @return WordPress Post Object
*/
function get_previous_post_looped() {
$post_type = get_post_type();
if ( get_adjacent_post( false, '', true) ) {
return get_previous_post();
} else {
$loop = new WP_Query( 'posts_per_page=1&order=DESC&post_type=' . $post_type );
$loop->the_post();
$postToReturn = get_post();
wp_reset_query();
return $postToReturn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment