SOLVED: Post navigation order by post title

Hi all,

The Post navigation next / previous seems to work with the post id.

Is there a way to modify the behaviour so than next and previous post is by Post Title in alphabetical order?

i want it to do Post A → Post B → Post C even if the post ID is not consecutive.

is that possible?

Hi Yann,

you can paste this code in the functions.php of your child theme:

function filter_next_post_sort($sort) {
    $sort = "ORDER BY p.post_title ASC LIMIT 1";
    return $sort;
}
function filter_next_post_where($where) {
    global $post, $wpdb;
    return $wpdb->prepare("WHERE p.post_title > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
}

function filter_previous_post_sort($sort) {
    $sort = "ORDER BY p.post_title DESC LIMIT 1";
    return $sort;
}
function filter_previous_post_where($where) {
    global $post, $wpdb;
    return $wpdb->prepare("WHERE p.post_title < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
}

add_filter('get_next_post_sort',   'filter_next_post_sort');
add_filter('get_next_post_where',  'filter_next_post_where');

add_filter('get_previous_post_sort',  'filter_previous_post_sort');
add_filter('get_previous_post_where', 'filter_previous_post_where');

If you want it only for a specific post type, take a look here.

Best regards,
timmse

2 Likes

Thank you timmse
You are a life saver :smiley:

Have a great day man

1 Like