-
-
Save 1wdtv/0bbeff43d8103ef0e284d24ede60aea6 to your computer and use it in GitHub Desktop.
[WordPress] change next / previous post link ordering
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 | |
/** | |
* Customize Adjacent Post Link Order | |
*/ | |
function my_custom_adjacent_post_where($sql) { | |
if ( !is_main_query() || !is_singular() ) | |
return $sql; | |
$the_post = get_post( get_the_ID() ); | |
$patterns = array(); | |
$patterns[] = '/post_date/'; | |
$patterns[] = '/\'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\'/'; | |
$replacements = array(); | |
$replacements[] = 'menu_order'; | |
$replacements[] = $the_post->menu_order; | |
return preg_replace( $patterns, $replacements, $sql ); | |
} | |
add_filter( 'get_next_post_where', 'my_custom_adjacent_post_where' ); | |
add_filter( 'get_previous_post_where', 'my_custom_adjacent_post_where' ); | |
function my_custom_adjacent_post_sort($sql) { | |
if ( !is_main_query() || !is_singular() ) | |
return $sql; | |
$pattern = '/post_date/'; | |
$replacement = 'menu_order'; | |
return preg_replace( $pattern, $replacement, $sql ); | |
} | |
add_filter( 'get_next_post_sort', 'my_custom_adjacent_post_sort' ); | |
add_filter( 'get_previous_post_sort', 'my_custom_adjacent_post_sort' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment