Skip to content

Instantly share code, notes, and snippets.

@amielucha
Created September 12, 2017 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amielucha/7a6f690ab14b67212049f287df686e67 to your computer and use it in GitHub Desktop.
Save amielucha/7a6f690ab14b67212049f287df686e67 to your computer and use it in GitHub Desktop.
WordPress: sort Previous and Next post by menu_order
<?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