Custom post type next previous post link when order posts by menu_order

There are 2 functions in WordPress to let you display previous and next post links: previous_post_link and next_post_link. In general, they work so well and really save a bunch time when coding a theme.

Just need to add this code in theme functions.php file to make it work correctly.


function myls_previous_lesson_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', 'myls_previous_lesson_where' );

function myls_next_lesson_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', 'myls_next_lesson_where' );

function myls_previous_lesson_sort() {
	return "ORDER BY p.menu_order desc LIMIT 1";
}
add_filter( 'get_previous_post_sort', 'myls_previous_lesson_sort' );

function myls_next_lesson_sort() {
	return "ORDER BY p.menu_order asc LIMIT 1";
}
add_filter( 'get_next_post_sort', 'myls_next_lesson_sort' );

https://codex.wordpress.org/Next_and_Previous_Links

Get the right prev/next post link when order posts by menu_order