Last active
March 5, 2024 11:46
-
-
Save wplit/005979a82f044f6e97c126a4825e9980 to your computer and use it in GitHub Desktop.
reverse order of WP's adjacent for posts
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 | |
add_action( 'get_previous_post_where', 'my_prev_post_where', 20 ); | |
function my_prev_post_where( $sql ){ | |
if ( !is_main_query() || !is_singular('post') ) { | |
return $sql; | |
} | |
return str_replace('<', '>', $sql); | |
} | |
add_action( 'get_next_post_where', 'my_next_post_where', 20 ); | |
function my_next_post_where( $sql ){ | |
if ( !is_main_query() || !is_singular('post') ) { | |
return $sql; | |
} | |
return str_replace('>', '<', $sql); | |
} | |
add_filter( 'get_previous_post_sort', 'my_previous_post_sort' ); | |
function my_previous_post_sort($sql) { | |
if ( !is_main_query() || !is_singular('post') ) { | |
return $sql; | |
} | |
return "ORDER BY p.post_date ASC LIMIT 1"; | |
} | |
add_filter( 'get_next_post_sort', 'my_next_post_sort' ); | |
function my_next_post_sort($sql) { | |
if ( !is_main_query() || !is_singular('post') ) { | |
return $sql; | |
} | |
return "ORDER BY p.post_date DESC LIMIT 1"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi David,
Thanks for this! 👍 It works great! However, I had to make some adjustments (with ChatGPT's help) to include the sorting of pages as well. This is the alternative from ChatGPT:
`add_action('get_previous_post_where', 'modify_previous_post_where', 20);
function modify_previous_post_where($where) {
// Check if it's a main query for a single post or page.
if (!is_main_query() || !(is_singular('post') || is_singular('page'))) {
return $where;
}
// Modify the WHERE clause to reverse the operation for descending order.
return str_replace('<', '>', $where);
}
add_action('get_next_post_where', 'modify_next_post_where', 20);
function modify_next_post_where($where) {
// Check if it's a main query for a single post or page.
if (!is_main_query() || !(is_singular('post') || is_singular('page'))) {
return $where;
}
// Modify the WHERE clause to ensure correct operation for descending order.
return str_replace('>', '<', $where);
}
add_filter('get_previous_post_sort', 'modify_previous_post_sort');
function modify_previous_post_sort($orderby) {
// Check if it's a main query for a single post or page.
if (!is_main_query() || !(is_singular('post') || is_singular('page'))) {
return $orderby;
}
// Force the sort order to descending for previous posts/pages.
return "ORDER BY p.post_date DESC LIMIT 1";
}
add_filter('get_next_post_sort', 'modify_next_post_sort');
function modify_next_post_sort($orderby) {
// Check if it's a main query for a single post or page.
if (!is_main_query() || !(is_singular('post') || is_singular('page'))) {
return $orderby;
}
// Force the sort order to descending for next posts/pages as well.
return "ORDER BY p.post_date DESC LIMIT 1";
}`