Get next post in the loop

Sometimes you need to access the next post in a loop, which is not created by Views, but when you are located on a Single Post.

For example, you want to display the Featured Image of the Post that comes next in the same Post Type, on the previous post.

The “next post” in the loop, can be accessed using the native WordPress function get_next_post()
https://codex.wordpress.org/Function_Reference/get_next_post

A ShortCode that returns the next post using this function can be crafted using these guidelines.
https://codex.wordpress.org/Shortcode_API

You should end up with:

function next_post_id( ) {
    $next_post_id = get_next_post()->ID;
    return $next_post_id;
}
add_shortcode( 'next-post-id', 'next_post_id' );

Now, you can use that in any ShortCode that supports an ID, in a single post, for example, to display the next’ posts’ image:

[wpv-post-featured-image item="[next_post_id]"]

Support Thread Example

Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments