WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] posts of same category in next_posts_link (8 posts)

  1. slavix
    Member
    Posted 5 years ago #

    Hello,
    I would like to provide links to next and previous posts of the same category on single posts page. Is there any way to have next_posts_links only use posts of same category and ordered by title? If not, how can I achieve the desired result?

    thanks
    Slava

  2. moshu
    Member
    Posted 5 years ago #

    Read the docs:
    Template_Tags/next_post_link - and its pair: previous...

  3. slavix
    Member
    Posted 5 years ago #

    Thanks for the hint. I looked at the link you provided and used this code

    <?php if ( in_category('68'): ?>
    	<div class="navigation">
    		<div class="alignleft"><?php previous_post_link('&laquo; %link', 'previous', true) ?></div>
    		<div class="alignright"><?php next_post_link('%link &raquo;', 'next', true) ?></div>
    	</div>
    <?php endif; ?>

    and it works, but I still do not know how to have the prev/next links to order posts by title.. any ideas please?

  4. moshu
    Member
    Posted 5 years ago #

    how to have the prev/next links to order posts by title..

    That sounds like a nonsense question.
    You have ONE post before (a.k.a "previous") and another ONE after (a.k.a. "next") - in relation to the post that is viewed. What is there to be ordered?

  5. slavix
    Member
    Posted 5 years ago #

    When determining which post to use as next or previous post the call to next_post_link should order posts by title instead of default behaviour: by date of creation.

    I found the answer to previous question, but now I am having problems implementing it. To change the ordering of posts in next_post_link I added a filter on 'get_{$adjacent}_post_sort' hook which is applied inside get_adjacent_post() when constructing the query.

    relevant code from get_adjacent_post() in wp-includes/link-template.php

    $adjacent = $previous ? 'previous' : 'next';
    $op = $previous ? '<' : '>';
    $order = $previous ? 'DESC' : 'ASC';
    
    $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
    $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories );
    $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
    
    return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");

    here is relevant plugin code: (WPD_print prints debuging messages to bottom of page. )

    function filter_next_post_sort($sort) {
           $sort = "ORDER BY p.post_title ASC LIMIT 1";
    	WPD_print('next sort: '.$sort);
    	return $sort;
    }
    
    function filter_previous_post_sort($sort) {
           $sort = "ORDER BY p.post_title DESC LIMIT 1";
    	WPD_print('previous sort: '.$sort);
            return $sort;
    }
    
    function filter_next_post_where($where) {
    	WPD_print('next where: '.$where);
    	return $where;
    }
    
    function filter_previous_post_where($where) {
       	WPD_print('previous where: '.$where);
    	return $where;
    }
    
    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');

    filter_next_post_sort and filter_previous_post_sort replace ordering to use post_title. filter_next_post_where and filter_previous_post_where need to modify the where clause to remove post_date restrictions. For some reason I can't get wordpress to call get_next_post_where and get_previous_post_where..

    get_next_post_sort and get_previous_post_sort get called with no problems and filter functions get called and print debugging message.

  6. slavix
    Member
    Posted 5 years ago #

    never mind I found the problem.

  7. slavix
    Member
    Posted 5 years ago #

    never mind. I found the problem. thanks

  8. Piet
    Member
    Posted 5 years ago #

    great solution, thanks!

    but Slavix, I must say that it would help a lot to paste the code correctly, you missed a ) in the first line...

    Finally after a couple of days searching I discovered it and it works indeed splendid!

    Cheers

  9. rorys
    Member
    Posted 5 years ago #

    I don't see how the above code works. You modify the ORDER but not the WHERE ... So for "next", you are going to get the next alphabetical post that is newer, and for previous you will get the previous alphabetical post that is older.

    Below is what I did to get it work:

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

Topic Closed

This topic has been closed to new replies.

About this Topic