Created
April 16, 2019 07:44
-
-
Save navnit-viradiya/243d8b2a6fb81d7b85b2705d9d4f525e to your computer and use it in GitHub Desktop.
Get the right prev/next post link when order posts by menu_order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function my_previous_post_where() { | |
global $post, $wpdb; | |
return $wpdb->prepare( "WHERE p.menu_order < %s AND p.post_type = %s AND p.post_status = 'publish'", $post->menu_order, $post->post_type); | |
} | |
add_filter( 'get_previous_post_where', 'my_previous_post_where' ); | |
function my_next_post_where() { | |
global $post, $wpdb; | |
return $wpdb->prepare( "WHERE p.menu_order > %s AND p.post_type = %s AND p.post_status = 'publish'", $post->menu_order, $post->post_type); | |
} | |
add_filter( 'get_next_post_where', 'my_next_post_where' ); | |
function my_previous_post_sort() { | |
return "ORDER BY p.menu_order desc LIMIT 1"; | |
} | |
add_filter( 'get_previous_post_sort', 'my_previous_post_sort' ); | |
function my_next_post_sort() { | |
return "ORDER BY p.menu_order asc LIMIT 1"; | |
} | |
add_filter( 'get_next_post_sort', 'my_next_post_sort' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment