I'm using the Twenty Fifteen (child) Theme and want to display the previous / next post links with the background image for my hierarchical custom post type.

It is being displayed, unfortunately the order seems completely wrong. I assume this is because the post are being selected by date and not by menu order?

I want the next / previous post to be like I have ordered them in the backend & menu, and it should be shown regardless of if its a parent, child or sibling to the current post.

The code for this is in the single.php:

the_post_navigation( array(
    'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next', 'twentyfifteen' ) . '</span> ' .
        '<span class="screen-reader-text">' . __( 'Next post:', 'twentyfifteen' ) . '</span> ' .
        '<span class="post-title">%title</span>',
    'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous', 'twentyfifteen' ) . '</span> ' .
        '<span class="screen-reader-text">' . __( 'Previous post:', 'twentyfifteen' ) . '</span> ' .
        '<span class="post-title">%title</span>',
) );

How can I get the posts in the correct order?

  • Note that the second answer is in fact the right one. – Thomas Mar 11 '16 at 12:33


According to the OP, this question/answers solved the issue


the_post_navigation(), right down to its very core, uses get_adjacent_post() to return and display the next and previous post to the currently viewed post.

By default, these adjacent posts are returned by post date. We can alter that by filtering the relevant ORDERBY clause of the SQL query. This filter we will be using is the get_{$adjacent}_post_sort filter where the dynamic {$adjacent} part refers to next or previous link.

Here is how the filter looks like:

apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post );

So, in a custom plugin or your theme's function file, we can do the following: (NOTE: This is untested, and we will only add our filter when a certain post type is displayed)

add_filter( 'get_next_post_sort',     'wpse_220361_adjacent_post_sort', 11, 2 );
add_filter( 'get_previous_post_sort', 'wpse_220361_adjacent_post_sort', 11, 2 );
function wpse_220361_adjacent_post_sort ( $orderby, $post )
    // Make sure we are on our desired post type
    if ( 'MY_CUSTOM_POST_TYPE_SLUG' !== $post->post_type )
        return $orderby;

    // We are on the desired post type, lets alter the SQL
    $orderby = str_replace( 'post_date', 'menu_order', $orderby );

    return $orderby;
| improve this answer | |
  • Thanks for that great explanation - unfortunately your solution doesn't seem to work quite yet. I've added the correct cpt slug (tested by echoing some string). Here's my site: wmkompendium.de All "Kapitel" are in chronological order except of "Startseite" and "Info-Center" which I have switched for testing purposes. Notice the menu is already in correct order - same as in the backend. – Thomas Mar 11 '16 at 12:06
  • Actually, it seems it's just a bit more complicated. I was looking at this question before, but could not get the accepted answer to work - but the second one seemed to get it right. wordpress.stackexchange.com/questions/73190/… – Thomas Mar 11 '16 at 12:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.