Tag Archives: get adjacent post

Query adjacent next and previous posts from current post in specific term

WordPress offers get_adjacent_post() to get the next post in the current term. It’s just like next and previous post link, but see’s what terms the current post is attached to, and stay’s within that term/terms. So if your current post is in terms ‘x’ and ‘y’, it will not pull any posts in ‘z’.

get_adjacent post() works great if all posts are only located in one term in a taxonomy and works ok when posts are in multiple categories. So if your posts are in multiple categories, you may not get the exact results you’re looking for with next/prev post I’ll show an example below.

If you’re fine going to the next or previous post even if the post is in multiple terms so that you may ‘jump’ to another term from which you came, the simply use WordPress’ get_adjacent_post()

But if your posts are in multiple terms and you want to stay in a specific term, you have to do a special query. The below query will pull the next and previous post in a post type within a specific taxonomy term. You can choose how many previous and next posts to pull by changing the limit and the get_post_siblings() number. This is a union query getting both results in one query.

<?php
function get_post_siblings( $limit = 1, $date = '' ) {
   global $wpdb, $post;
	
  // Custom Category
  $taxonomy = 'CUSTOM_TAXONOMY';
  // Custom Post Type
  $postType = 'CUSTOM_POSTTYPE';
  // SSpecific Term ID
  $term_id = '18';
  
  	if( empty( $date ) )
        $date = $post->post_date;

    $limit = absint( $limit );
    if( !$limit )
        return;

    $p = $wpdb->get_results( "
    (
        SELECT 
            p1.post_title, 
            p1.post_date,
            p1.ID
        FROM 
            $wpdb->posts p1 
		INNER JOIN wp_term_relationships 
		AS tr 
		ON p1.ID = tr.object_id 
		INNER JOIN wp_term_taxonomy tt 
		ON tr.term_taxonomy_id = tt.term_taxonomy_id 
        WHERE 
            p1.post_date < '$date' AND 
            p1.post_type = '$postType' AND 
            p1.post_status = 'publish' 
		AND tt.taxonomy = '$taxonomy' 
		AND tt.term_id 
		IN ($term_id) 
        ORDER by 
            p1.post_date DESC
        LIMIT 
            $limit
    )
    UNION 
    (
        SELECT 
            p2.post_title, 
            p2.post_date,
            p2.ID 
        FROM 
            $wpdb->posts p2 
		 INNER JOIN wp_term_relationships 
		AS tr 
		ON p2.ID = tr.object_id 
		INNER JOIN wp_term_taxonomy tt 
		ON tr.term_taxonomy_id = tt.term_taxonomy_id
        WHERE 
            p2.post_date > '$date' AND 
            p2.post_type = '$postType' AND 
            p2.post_status = 'publish' 
		AND tt.taxonomy = '$taxonomy' 
		AND tt.term_id 
		IN ($term_id)	
		ORDER by
            p2.post_date ASC
        LIMIT 
            $limit
    ) 
    ORDER by post_date ASC
    " );
    $i = 0;
    $adjacents = array();
    for( $c = count($p); $i < $c; $i++ )
        if( $i < $limit )
            $adjacents['prev'][] = $p[$i];
        else
            $adjacents['next'][] = $p[$i];

    return $adjacents;
}
?>

<div class="proj-pagi-prev">
	<?php 
    $siblings = get_post_siblings( 1 ); 
     $prev = $siblings['prev'];
    
    foreach( $prev as $p )
        $permalink = get_permalink( $p->ID );
            echo "<a href='" . $permalink . "'><span class='plain-text'>Previous</span> PROJECT</a>";
    ?>
</div><!-- proj pagi prev -->


<div class="proj-pagi-next">
	<?php 
    $next = $siblings['next'];
    
    foreach( $next as $p )
        $permalink = get_permalink( $p->ID );
             echo "<a href='" . $permalink . "'><span class='plain-text'>Next</span> PROJECT</a>";
    ?>
</div><!-- proj-pagi-next -->

If you want to break it out in two separate queries, you can use this one. Added to this one is a url variable (cat) which requests the passed url, which is the taxonomy term from where you came. You would of course have to add that to the link you came from in some fashion.

<?php
function get_post_siblings( $limit = 1, $date = '' ) {
   global $wpdb, $post;
       
  // Custom Category
  $taxonomy = 'CUSTOM_TAXONOMY';
  // Custom Post Type
  $postType = 'CUSTOM_POSTTYPE';
  // Get term id value from incoming URL
  $term_id = $_REQUEST['cat'];
  //echo $term_id;
  $terms = get_the_terms( $post->ID, $taxonomy );
  // echo $terms->term_id;
    if( empty( $date ) )
        $date = $post->post_date;
 
    $limit = absint( $limit );
    if( !$limit )
        return;
        
    // Get previous posts into $p
    $p = $wpdb->get_results( "
   (
       SELECT
           p1.post_title,
           p1.post_date,
           p1.ID
       FROM
           $wpdb->posts p1
                INNER JOIN wp_term_relationships
                AS tr
                ON p1.ID = tr.object_id
                INNER JOIN wp_term_taxonomy tt
                ON tr.term_taxonomy_id = tt.term_taxonomy_id
       WHERE
           p1.post_date < '$date' AND
           p1.post_type = '$postType' AND
           p1.post_status = 'publish'
                AND tt.taxonomy = '$taxonomy'
                AND tt.term_id
                IN ($term_id)
       ORDER by p1.post_date DESC
       LIMIT $limit
	   )
        " );
   
    // Get next posts into $n
    $n = $wpdb->get_results( "
       (
       SELECT
           p2.post_title,
           p2.post_date,
           p2.ID
       FROM
           $wpdb->posts p2
                 INNER JOIN wp_term_relationships
                AS tr
                ON p2.ID = tr.object_id
                INNER JOIN wp_term_taxonomy tt
                ON tr.term_taxonomy_id = tt.term_taxonomy_id
       WHERE
           p2.post_date > '$date' AND
           p2.post_type = '$postType' AND
           p2.post_status = 'publish'
                AND tt.taxonomy = '$taxonomy'
                AND tt.term_id
                IN ($term_id)  
       ORDER by post_date ASC
       LIMIT $limit
       )
       " );
   
    $adjacents = array();
    $adjacents['prev'] = array();
    $adjacents['next'] = array();
    
    for( $i=0; $i<count($p); $i++ ) {
        $adjacents['prev'][] = $p[$i];
    }
    
    for( $i=0; $i<count($n); $i++ ) {
        $adjacents['next'][] = $n[$i];
    }    
 
    return $adjacents;
}
 
?>
 
<div class="proj-pagi-prev">
      <?php
	   $siblings = get_post_siblings( 1 );
       $next = $siblings['next'];
   
    foreach( $next as $n )
        $term_id = $_REQUEST['cat'];
        $permalink = get_permalink( $n->ID );
        $link = $permalink . "?cat=" . $term_id;
		if($n != "") {
             echo "<a href='" . $link . "'><span class='plain-text'>Previous</span> PROJECT</a>";
		}
    ?>
</div><!-- proj pagi prev -->
 
 
<div class="proj-pagi-next">
   <?php
   
   
    $prev = $siblings['prev'];
   
    foreach( $prev as $p )
        $term_id = $_REQUEST['cat'];
        $permalink = get_permalink( $p->ID );
        $link = $permalink . "?cat=" . $term_id;
		if($p != "") {
            echo "<a href='" . $link . "'><span class='plain-text'>Next</span> PROJECT</a>";
		}
    ?>
       
</div><!-- proj-pagi-next -->