ControlledChaos of Controlled Chaos Design
8/9/2016 - 6:46 PM

Change next_ & previous_post_link to fetch posts by menu order rather than by ID - Recognizes custom sorting.

Change next_ & previous_post_link to fetch posts by menu order rather than by ID - Recognizes custom sorting.

<?php

function ccd_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', 'ccd_previous_post_where' );

function ccd_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', 'ccd_next_post_where' );

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

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

?>

Next-Prev Posts by Menu Order

WordPress Snippet