Sorting out the next / previous links on WordPress

Fellow coders and others :-) , I’m sure there will/has been a point where the next / previous links on the wordpress category blog dint not match the category listing. The solution , once figured out is fairly simple to implement .Ther’s actually no need to actually install a third party plugin to SORT this out.

The problem: When you have more than 1 category on your blog the next/ previous links dont restrict the same parent catgory. by default, wordpress disregards categories and just points to the next/previous published post.

The solution:
We need to hook into default wordpress sort and make sure the list is sorted by date.

add_filter('posts_orderby', 'kporderposts');
 function kporderposts( $query ){
if($query=="wp_posts.post_date DESC"){
 $query.=", wp_posts.ID desc";
}
return $query;
}

Next, is the function to display our own function to show the improvised next/previous links

function post_nextpre($p){
 global $wpdb;
$results=array("next"=>false,"prev"=>false);
 //get taxamony/
 $sql=" SELECT p.post_title , p.ID , tr.*
 FROM {$wpdb->posts} p
 join {$wpdb->term_relationships} tr
 ON(tr.object_id=p.ID)
WHERE
 p.ID=".((int)$p)."
 AND p.post_status='publish';";
 $r = $wpdb->get_results( $sql, OBJECT);
 $r=$r[0];
// get current
 $sql="SELECT p.*, tr.*
 FROM {$wpdb->term_relationships} tr
 JOIN {$wpdb->posts} p
ON( p.ID = tr.object_id)
WHERE tr.object_id =".((int)$p)."
 AND tr.term_taxonomy_id =".$r->term_taxonomy_id."
 AND p.post_status='publish';";
$current = $wpdb->get_results( $sql, OBJECT);
 $current=$current[0];
/// get siblings
 $sql="SELECT p.ID
 FROM {$wpdb->term_relationships} tr
 JOIN {$wpdb->posts} p
 ON( p.ID = tr.object_id)
WHERE
 p.post_date='".$current->post_date."'
 AND tr.term_taxonomy_id =".$r->term_taxonomy_id."
 AND p.post_status='publish'
 ORDER BY p.post_date desc, p.ID desc";
$duplicates=array();
 $tmp= $wpdb->get_results( $sql, OBJECT);
 $offset=0;
foreach($tmp as $duplicate)
 {
 $duplicates[]=$duplicate->ID;
 }
 if(sizeof($duplicates)>1)
 {
 $offset=array_search($current->ID,$duplicates);
 if($offset>0)
 {
 $dprev=$offset-1;
 $results['prev']= get_permalink($duplicates[$dprev]);
 }
 if($offset<(sizeof($duplicates)-1)) { $dnext=$offset+1; $results['next']= get_permalink($duplicates[$dnext]); } } if(!$results['next']){ $sql=" SELECT p.ID FROM {$wpdb->term_relationships} tr
 JOIN {$wpdb->posts} p
 ON( p.ID = tr.object_id)
WHERE
 p.post_date<'".$current->post_date."'
 AND tr.term_taxonomy_id =".$r->term_taxonomy_id."
 AND p.post_status='publish'
 ORDER BY p.post_date desc, p.ID desc
 LIMIT 0 , 1";
$sql_result = $wpdb->get_results( $sql, OBJECT);
 if($sql_result[0]->ID)
 {
 $results['next']=get_permalink( $sql_result[0]->ID);
 }
 }
 if(!$results['prev']){
 $sql="SELECT p.ID
 FROM {$wpdb->term_relationships} tr
 JOIN {$wpdb->posts} p
 ON( p.ID = tr.object_id)
WHERE
 p.post_date>'".$current->post_date."'
 AND tr.term_taxonomy_id =".$r->term_taxonomy_id."
 AND p.post_status='publish'
 ORDER BY p.post_date asc, p.ID asc
 LIMIT 0 , 1";
 $sql_result = $wpdb->get_results( $sql, OBJECT);
 if($sql_result[0]->ID)
 {
 $results['prev']=get_permalink( $sql_result[0]->ID);
 }
 }
 return $results;
 }

My dear fellow nerds , if your wondering what the “find siblings” code is doing there; It’s because we are sorting by date and technically a post could be published at the very same second in time. there could be more that one post published on the same date.

Let me know if there’s a better way and feel free to modify the code as it ┬áneeds optimization.