• Resolved Diego Toledo

    (@dormiu)


    I’m exhaustively searching for a method to provide different Next and Previous Links at Single Posts.

    By DEFAULT it:

    • Is chronological ordered
    • Links to posts from all blog categories

    But I NEED it:

    • Alphabetically ordered
    • Linking to posts from same category only

    I found this code that let me now order the Next/Prev Links alhpabetically however I still can’t show links only from same category of the post.

    function filter_next_post_sort($sort) {
        if (get_post_type($post) == 'post') {
            $sort = "ORDER BY p.post_title ASC LIMIT 1";
        }
        else{
            $sort = "ORDER BY p.post_date ASC LIMIT 1";
        }
        return $sort;
    }
    function filter_next_post_where($where) {
        global $post, $wpdb;
        if (get_post_type($post) == 'post') {
            return $wpdb->prepare("WHERE p.post_title > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
        }
        else{
            return $wpdb->prepare( "WHERE p.post_date > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'", $post->post_date, $post->post_type );
        }
    }
    
    function filter_previous_post_sort($sort) {
        if (get_post_type($post) == 'post') {
            $sort = "ORDER BY p.post_title DESC LIMIT 1";
        }
        else{
            $sort = "ORDER BY p.post_date DESC LIMIT 1";
        }
        return $sort;
    }
    function filter_previous_post_where($where) {
        global $post, $wpdb;
        if (get_post_type($post) == 'post') {
            return $wpdb->prepare("WHERE p.post_title < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
        }
        else{
            return $wpdb->prepare( "WHERE p.post_date < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'", $post->post_date, $post->post_type );
        }
    }
    
    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');

    Note: Every post is assigned to only one category.

    THANK YOU FOR HELP ME!

Viewing 6 replies - 1 through 6 (of 6 total)
  • you need to create something like this.
    So in your case in_same_cat will be TRUE… something like:
    <?php next_post_link(‘%link’, ‘Next’, TRUE); ?>

    Thread Starter Diego Toledo

    (@dormiu)

    This solution provides the possibility to link only to posts from same category, but NOT alphabetically. 🙁

    Thread Starter Diego Toledo

    (@dormiu)

    Forgot to say: I already tried to combine both methods (the code that I pasted above and your suggestion) but didn’t work for me.

    if you have time this will help you , but you must write the code for you.
    or you can search for an plugin

    Thread Starter Diego Toledo

    (@dormiu)

    Zota, thank you about your responses but it does not solve my problem.

    I need the Next / Previous Links displaying links ALPHABETICALLY and only FROM SAME CATEGORY of the post.

    I’m not a developer but I found two codes and I think if I could merge both the problem would be solved.

    CODE 1Turn Next/Prev links alphabetcally, but not from same category (source)

    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');

    CODE 2Turn Next/Prev links from same category, but not alphabetically (source)

    add_filter( 'get_next_post_join', 'navigate_in_same_taxonomy_join', 20);
    add_filter( 'get_previous_post_join', 'navigate_in_same_taxonomy_join', 20 );
    function navigate_in_same_taxonomy_join() {
     global $wpdb;
     return " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
    }
    
    add_filter( 'get_next_post_where' , 'navigate_in_same_taxonomy_where' );
    add_filter( 'get_previous_post_where' , 'navigate_in_same_taxonomy_where' );
    function navigate_in_same_taxonomy_where( $original ) {
     global $wpdb, $post;
     $where = '';
     $taxonomy   = 'category';
     $op = ('get_previous_post_where' == current_filter()) ? '<' : '>';
     $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
     if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
     return $original ;
    
     $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    
     $term_array = array_map( 'intval', $term_array );
    
     if ( ! $term_array || is_wp_error( $term_array ) )
     return $original ;
    
     $where = " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
     return $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $post->post_date, $post->post_type );
    }

    Thanks again!

    Thread Starter Diego Toledo

    (@dormiu)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Next / Previous Post Links: Alphabetically and from same Category’ is closed to new replies.