• I’m having some trouble using the function get_previous_post() when trying to implement post navigation on the single.php page.

    At the moment, it is retrieving the post that is currently being displayed on the single.php page.

    I am using get_next_post() function in conjunction with this and that function seems to work perfectly fine, just trouble with get_previous_post().

    Has anyone else encountered trouble of the same kind and could give me some advice?

    An excerpt of the code used:

    <!– Previous post –>
    <?php

    // Get previous post
    $previousPost = get_previous_post();

    // Check to see if it is there is a post
    if (!empty($previousPost)) {
    // Previous post available

    // Get full post data
    $post = get_post($previousPost->ID);

    setup_postdata($post);

    ?>
    (Display post in HTML below)

    As I said, the data in $post is the same of that of the data of the page’s current post. I know that there isn’t a problem with double assignment or overriding with the $post variable as I am using it before for get_next_post() with no problems.

    Any help will be greatly appreciated.
    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • How and where are you checking? Your use of $post (which is a global in the current context) and setup_postdata can really throw things off.

    What you should be doing to test this is:

    $current_post = get_post(); $previous_post = get_previous_post();
    assert( $current_post->ID != $previous_post->ID );

    Nothing in between and no misuse of the $post global. Does the above work?

    Thread Starter alifhughes

    (@alifhughes)

    Thanks for the response!

    I’ve used $post outside of the main loop, and used wp_reset_postdata(); after the main loop so thought that it would be fine to use it as I have been, which is why I am confused to why it isn’t working.

    When I use your test, it returns true, I did:

    if (assert($current_post->ID != $previous_post->ID )) { die(‘Same post’); }

    and it did display the die message.

    Any suggestions ?

    No, that’s not how assert works. http://php.net/manual/en/function.assert.php

    assert will return true if the condition is true. So:

    if (assert($current_post->ID != $previous_post->ID )) { die('Not the same post'); }

    Or perhaps just:

    if ($current_post->ID == $previous_post->ID) { die('Same post'); }

    It won’t die there. The fact that your code above died and displayed the message means that they’re actually not the same post, and that everything worked.

    So please don’t use the global $post or reset_postdata, you’re mixing everything up it seems, and polluting the namespace.

    Thread Starter alifhughes

    (@alifhughes)

    Ah sorry, thank you for your detailed answer, I appreciate it.

    You are correct though, it didn’t die when I did the second if statement. However, it still is showing the same post in my previous post display.

    I think you are right, I will not use the global namespace and try tidy things up, then it might start to work.

    Thanks

    Thread Starter alifhughes

    (@alifhughes)

    Is there anything you would recommend to get the previous post and all of its data? I would use just get_previous_post() but I want post data such as featured image.

    Thanks

    get_previous_post is a valid way to get the previous post. The featured image can be retrieved using https://developer.ww.wp.xz.cn/reference/functions/get_the_post_thumbnail/ and giving it the ID of the previous post.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘get_previous_post() retrieves current post??’ is closed to new replies.