-
-
Save ajithrn/03a8fbf0e8aab0d429ddebe6b25bf019 to your computer and use it in GitHub Desktop.
WP : next / prev post link
This file contains hidden or 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 | |
/** | |
* Return the right previous_post_link / next_post_link when change posts order | |
* ref: https://1fix.io/blog/2014/09/09/get-right-previous_post_link-when-order-posts-by-menu_order/ | |
* more reading : http://wordpress.stackexchange.com/questions/73190/can-the-next-prev-post-links-be-ordered-by-menu-order-or-by-a-meta-key | |
*/ | |
function kms_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', __NAMESPACE__.'\\kms_previous_post_where' ); | |
function kms_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', __NAMESPACE__.'\\kms_next_post_where' ); | |
function kms_previous_post_sort() { | |
return "ORDER BY p.menu_order desc LIMIT 1"; | |
} | |
add_filter( 'get_previous_post_sort', __NAMESPACE__.'\\kms_previous_post_sort' ); | |
function kms_next_post_sort() { | |
return "ORDER BY p.menu_order asc LIMIT 1"; | |
} | |
add_filter( 'get_next_post_sort', __NAMESPACE__.'\\kms_next_post_sort' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment