I’ve been trying to retrieve the next and previous post in one of my Wordpress templates. Please note that I want to get the full post object instead of just the pre-formatted links to the posts.
The problem I face is that whenever I call these functions wrong posts are returned. They are in the same post type, but they are off by like a few ids (say 3/4).
// Get next and previous link
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
var_dump($post);
var_dump(get_previous_post());
}
}
The first variable dump returns me the correct post object. The second one returns a page that's around 4 IDs (they range from 64 - 60) off...
I tried calling the functions both with and without parameters, it returned false posts in both situations.
Hope someone has an answer.
$post
to contain the correct post that you are trying to fetch next/prev from, verify that this contains the correct post object. A secondary query can overwrite this value if it's not reset correctly.var_dump($post)
it returns me the correct object