Skip to content

Instantly share code, notes, and snippets.

@wplit
Last active March 5, 2024 11:46
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 wplit/005979a82f044f6e97c126a4825e9980 to your computer and use it in GitHub Desktop.
Save wplit/005979a82f044f6e97c126a4825e9980 to your computer and use it in GitHub Desktop.
reverse order of WP's adjacent for posts
<?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";
}
@AnthonyBrowneCreative
Copy link

AnthonyBrowneCreative commented Mar 5, 2024

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";
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment