[Support request] Change the post navigation order…

Home Forums Support Change the post navigation order…

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #1160676
    Yannick

    Hi!

    How can I change the post navigation order? See URL provided.

    At the bottom, it displays the previous post as “Cuisine 9” and the next to “Cuisine 7”.
    It should be the other way around (7 should be previous, and 9 next). I’ve tried playing with get_adjacent_post, but no success…

    #1160770
    David
    Staff
    Customer Support

    Hi there,

    Maybe alphabetizing the main query would be the easiest method:

    https://codex.wordpress.org/Alphabetizing_Posts

    #1160870
    Yannick

    Not very helpful, as I already change the way the posts are ordered with add_filter('posts_orderby', ..);, like so:

    global $wpdb;
    return "LENGTH($wpdb->posts.post_title) ASC, $wpdb->posts.post_title ASC";
    return $orderby;

    Note that I use LENGTH for natural sorting, to avoid sorting like so: Cuisine 1, Cuisine 10, Cuisine 2…

    #1161108
    Tom
    Lead Developer
    Lead Developer

    Tricky one..

    Try adding this PHP:

    add_filter( 'generate_post_navigation_args', function( $args ) {
        $args['previous_format'] = '<div class="nav-next">' . generate_get_svg_icon( 'arrow' ) . '<span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">%link</span></div>';
        $args['next_format'] = '<div class="nav-previous">' . generate_get_svg_icon( 'arrow' ) . '<span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">%link</span></div>';
    
        return $args;
    } );
    #1161126
    Yannick

    Yep, that works. Didn’t think of this logic 🙂

    Just need to figure out how to have the navigation also sort naturally (because of numbers in titles). So I see “Cuisine 1”, “Cuisine 10”, “Cuisine 11”, “Cuisine 2”, “Cuisine 3”, … (note that it sorts fine on the blog/home page).

    #1161625
    Yannick

    Tom: any easy way to switch how they are displayed? Right now, the “next” is above the “previous” (I want it the other way around)…

    #1161720
    David
    Staff
    Customer Support

    If you’re ok with CSS to re-order them then this:

    .site-main .post-navigation {
        display: flex;
        flex-direction: column-reverse;
    }
    #1161730
    Yannick

    I always prefer PHP as it is more robust than CSS, but I’ll use this while I wait for Tom.

    #1162092
    Tom
    Lead Developer
    Lead Developer

    If you need PHP, you would have to overwrite the entire function, which would be a shame. In this case, I think CSS is the preferred method 🙂

    #1162102
    Yannick

    Thank Tom. Any idea how I can take into consideration the “NATURAL” sorting for the post navigation?

    As I mentionned above, I’m using the following to order posts:

    global $wpdb;
    return “LENGTH($wpdb->posts.post_title) ASC, $wpdb->posts.post_title ASC”;
    return $orderby;

    Works well on archives, blog/home page (sorts like so: Cuisine 1, Cuisine 2, Cuisine 3 and not Cuisine 1, Cuisine 10, Cuisine 2, Cuisine 3…), but the post navigation doesn’t seem to take this into consideration. So at the bottom of Cuisine 2, I get Cuisine 1 (previous) and Cuisine 20 (next), when it should be Cuisine 3.

    #1162369
    Tom
    Lead Developer
    Lead Developer

    Where are you adding that code?

    What if you do this instead?:

    add_action( 'pre_get_posts', function( $query ) { 
        if ( $query->is_main_query() && ! is_admin() ) {
            $query->set( 'orderby', 'title' );
            $query->set( 'order', 'ASC' );
        }
    } );
    #1162370
    Yannick

    It has to be sorted by title, and take into consideration natural sorting (https://en.wikipedia.org/wiki/Natural_sort_order)… I’m a programmer, and I know what needs to be done, but I can’t figure out where it needs to be called, or what hook/filter I need to use.

    Like I said, it’s sorted properly on the blog/home page, in the archives, but not for the post navigation…

    #1162374
    Tom
    Lead Developer
    Lead Developer

    If it’s possible, you’d need to alter the actual query, which my code above does: https://generatepress.com/forums/topic/change-the-post-navigation-order/#post-1162369

    I’m not 100% sure if it will work, but that query should alter every query on the site to orderby title.

    #1162376
    Yannick

    Yeah. The problem is the natural sorting issue…
    I would get C1, C10, C11, C2… and not C1, C2, …, C10, C11.

    #1162380
    Tom
    Lead Developer
    Lead Developer

    Perhaps this post will help?: https://wordpress.stackexchange.com/a/73194

    The post navigation is using those functions, so your answer is likely there if you update the query to match what you’re after.

Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.