1

I am using get_adjacent_post() function to display links to a previous and next post. It is a custom post which also has a custom taxonomy.

This is the code I am using:

$prev = get_adjacent_post(false, '', true);
$next = get_adjacent_post(false, '', false);

if($prev){
    $url = get_permalink($prev->ID);            
    echo '<a href="' . $url . '">Previous</a>';
}

if($next) {
    $url = get_permalink($next->ID);            
    echo '<a href="' . $url . '">Next</a>';
} 

As far as I understand, the parameters in the get_adjecent_post() function should consider all posts, since I am not restricting the selection to any taxonomy. However, it shows weird results. Currently I have 15 posts of this post type, the latest one only shows "previous" button (which is OK), but the second-latest only shows "next" button, as if there were only two posts. When I open another post manually I can go in both directions, but I am not able to go through all of the posts - as if there were some groups of posts created, and the get_adjecent_post() function only searches within these groups.

I am using Post Types Order plugin to customize the order of created posts and I think there could be some conflict.

Is something wrong in my code? Am I using the function in the wrong manner?

  • 1
    By default get_adjacent_post orders posts by post_date, so it should work just fine. But it allows to change that order using filters, so maybe PTO breaks it... Does it work when you disable PTO? – Krzysiek Dróżdż Sep 26 '18 at 7:31
  • @KrzysiekDróżdż yes, it is indeed a PTO issue. After deactivation it works great. Is there any native WP function that gets next and previous post that is not in conflict with PTO? (I know this is more of a PTO support forum question) – Kristián Filo Sep 28 '18 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.