Adjacent post by alphabetical order in Wordpress


/ Published in: PHP
Save to your folder(s)

Get next and prev posts in Wordpress by alphabetical order.


Copy this code and paste it in your HTML
  1. function filter_next_post_sort($sort) {
  2. $sort = "ORDER BY p.post_title ASC LIMIT 1";
  3. return $sort;
  4. }
  5. function filter_next_post_where($where) {
  6. global $post, $wpdb;
  7.  
  8. return $wpdb->prepare("WHERE p.post_title > '%s' AND p.post_type = 'post' AND p.post_status = 'publish'",$post->post_title);
  9. }
  10.  
  11. function filter_previous_post_sort($sort) {
  12. $sort = "ORDER BY p.post_title DESC LIMIT 1";
  13. return $sort;
  14. }
  15. function filter_previous_post_where($where) {
  16. global $post, $wpdb;
  17.  
  18. return $wpdb->prepare("WHERE p.post_title < '%s' AND p.post_type = 'post' AND p.post_status = 'publish'",$post->post_title);
  19. }
  20.  
  21. add_filter('get_next_post_sort', 'filter_next_post_sort');
  22. add_filter('get_next_post_where', 'filter_next_post_where');
  23.  
  24. add_filter('get_previous_post_sort', 'filter_previous_post_sort');
  25. add_filter('get_previous_post_where', 'filter_previous_post_where');

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.