Last active
June 26, 2023 22:56
-
-
Save hissy/3613306 to your computer and use it in GitHub Desktop.
[WordPress] change next / previous post link ordering
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 | |
/** | |
* 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
カスタム投稿タイプなどで、アーカイブ等で 'orderby' => 'menu_order' で表示しているサイトで、 previous_post_link() next_post_link() を使う方法