Order by post_date does not work in Previous and Next post pagination

I have added a previous and next pagination with thumbnail for my single post template but it does not showing the correct previous and next post. Also, saw its showing the previous and next post order by post ID and tried to change the order by post_date but it does not work. Below are the code of post navigation. Can someone help me please...

function wpse73190_adjacent_post_sort( $orderby ){
    return ORDER BY p.post_date DESC LIMIT 1;
}
add_filter( 'get_previous_post_sort', 'wpse73190_adjacent_post_sort' );
add_filter( 'get_next_post_sort', 'wpse73190_adjacent_post_sort' );

function wpsites_image_nav_links() {
    if (is_singular('post')) {
        $excluded_terms = '39';
        echo 'div class=nav_container';
        $prev_post = get_previous_post($excluded_terms);
        $next_post = get_next_post($excluded_terms);
        if (!empty( $prev_post )): ?
            ?php $prevthumb = get_the_post_thumbnail_url( $prev_post-ID, 'full' ); ?
            ?php echo'span style=background:url('.$prevthumb.') class=single-post-nav previous-post-link'; ?
            a href=?php echo $prev_post-guid ?
            ?php echo div id='borderLeft'/divdiv class='shape' style='left:0; z-index:1; position: absolute; top: -15px;'
            div id='curved-corner-bottomleft'/div
            div id='curved-corner-topleft'/div
            /div; ?   
            ?php echo p.$prev_post-post_title./p; ?
            /a
            ?php echo'/span'; ?
        ?php endif; ?
        
        ?php if (!empty( $next_post )): ?
            ?php $nextthumb = get_the_post_thumbnail_url( $next_post-ID, 'full' ); ?
            ?php echo'span style=background:url('.$nextthumb.') class=single-post-nav next-post-link'; ?
            a href=?php echo esc_url( get_permalink( $next_post-ID ) ); ?
            ?php echo div id='borderRight'/divdiv class='shape' style='right:0; z-index:1; position: absolute; top: -15px;'
            div id='curved-corner-bottomright'/div
            div id='curved-corner-topright'/div
            /div; ?
            ?php echo p.esc_attr( $next_post-post_title )./p; ?
            /a
            ?php echo '/span'; ?
        ?php endif; ?
        ?php echo '/div'; ?
    ?php  }
}

Topic next-post-link previous-post-link plugin-development plugins Wordpress

Category Web


$excluded_terms should be the 2nd arg to get_previous_post etc, not the first. Also those filters should not be required.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.