Title: Adjacent post in same category   same post format
Last modified: August 21, 2016

---

# Adjacent post in same category same post format

 *  [bobibrown0](https://wordpress.org/support/users/bobibrown0/)
 * (@bobibrown0)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/adjacent-post-in-same-category-same-post-format/)
 * Hello,
 * I am trying to modify the function “get_adjacent_post” to add another boolean
   variable to enable same post format filtering (I’ll create a new function in 
   function.php)
 * I went into the /wp-includes/link-template.php file to find the function but 
   it looks more complicated than I thought.
    Post format entry is not in the post
   table and I can’t comprehend the sql request in this function very well.
 * I searched online, but it seems no one ever tried this.
 * this is the original function:
 *     ```
       function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) {
       	global $wpdb;
   
       	if ( ! $post = get_post() )
       		return null;
   
       	$current_post_date = $post->post_date;
   
       	$join = '';
       	$posts_in_ex_cats_sql = '';
       	if ( $in_same_cat || ! empty( $excluded_categories ) ) {
       		$join = " 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";
   
       		if ( $in_same_cat ) {
       			if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) )
       				return '';
       			$cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
       			if ( ! $cat_array || is_wp_error( $cat_array ) )
       				return '';
       			$join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
       		}
   
       		$posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
       		if ( ! empty( $excluded_categories ) ) {
       			if ( ! is_array( $excluded_categories ) ) {
       				// back-compat, $excluded_categories used to be IDs separated by " and "
       				if ( strpos( $excluded_categories, ' and ' ) !== false ) {
       					_deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
       					$excluded_categories = explode( ' and ', $excluded_categories );
       				} else {
       					$excluded_categories = explode( ',', $excluded_categories );
       				}
       			}
   
       			$excluded_categories = array_map( 'intval', $excluded_categories );
   
       			if ( ! empty( $cat_array ) ) {
       				$excluded_categories = array_diff($excluded_categories, $cat_array);
       				$posts_in_ex_cats_sql = '';
       			}
   
       			if ( !empty($excluded_categories) ) {
       				$posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
       			}
       		}
       	}
   
       	$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 = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );
       	$sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
   
       	$query = "SELECT p.id FROM $wpdb->posts AS p $join $where $sort";
       	$query_key = 'adjacent_post_' . md5($query);
       	$result = wp_cache_get($query_key, 'counts');
       	if ( false !== $result ) {
       		if ( $result )
       			$result = get_post( $result );
       		return $result;
       	}
   
       	$result = $wpdb->get_var( $query );
       	if ( null === $result )
       		$result = '';
   
       	wp_cache_set($query_key, $result, 'counts');
   
       	if ( $result )
       		$result = get_post( $result );
   
       	return $result;
       }
       ```
   
 * I guess I will have to do something like this:
 *     ```
       function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true, $same_format = false ) {
       .
       .
       .
       if ($same_format) {
       	$join.='....';
       .
       .
       .
       }
       ```
   
 * If someone could help me find the correct junction, it would be great. It looks
   too messy for me.

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [bobibrown0](https://wordpress.org/support/users/bobibrown0/)
 * (@bobibrown0)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/adjacent-post-in-same-category-same-post-format/#post-4012505)
 * Ok actually it will be harder than I thought.
 * the junction is the following:
 * `$join = " 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";`
 * I made some test by adding the following lines right after:
 *     ```
       if ( $same_format ) {
       			$join .= " INNER JOIN $wpdb->terms AS t ON tr.term_taxonomy_id = t.term_id ";
       		}
       ```
   
 * and after the condition for $in_same_cat :
 *     ```
       if ( $same_format ) {
       			$format = get_post_format( $post->ID );
       			if ( empty($format) || is_wp_error( $format ) )
       				return '';
       			$join .= " AND t.name = 'post-format-".$format."'";
       		}
       ```
   
 * The thing is that it will never work since I need to choose between taxonomy “
   category” or “post_format” and term_id “category_ID:XXX” and “post-format-ID:
   XXX”
 * can’t have both at the same time or I will have duplicate entries… plus I need
   to get the post-format ID.
 *  Thread Starter [bobibrown0](https://wordpress.org/support/users/bobibrown0/)
 * (@bobibrown0)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/adjacent-post-in-same-category-same-post-format/#post-4012609)
 * OK, I made it!! I change the method to this:
 *     ```
       if ( $in_same_cat || ! empty( $excluded_categories ) ) {
       		$join = " 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";
   
       		if ( $in_same_cat ) {
       			if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) )
       				return '';
       			$cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
       			if ( ! $cat_array || is_wp_error( $cat_array ) )
       				return '';
       			$join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
       		}
   
       		if ( $same_format ) {
       			$join .= " AND p.ID IN (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id=420 ) ";
       		}
   
       .
       .
       .
       .
       ```
   
 * I don’t know if it’s the best solution to make it work, especially when I’ll 
   have more posts, but it works for now.
    I had to find the current post format
   id, but instead I actually only need to make sure it is in one specific format(
   id:420).
 * I hope it will help someone.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Adjacent post in same category same post format’ is closed to new replies.

## Tags

 * [adjacent](https://wordpress.org/support/topic-tag/adjacent/)
 * [format](https://wordpress.org/support/topic-tag/format/)
 * [next](https://wordpress.org/support/topic-tag/next/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [previous](https://wordpress.org/support/topic-tag/previous/)
 * [same](https://wordpress.org/support/topic-tag/same/)

 * 2 replies
 * 1 participant
 * Last reply from: [bobibrown0](https://wordpress.org/support/users/bobibrown0/)
 * Last activity: [12 years, 9 months ago](https://wordpress.org/support/topic/adjacent-post-in-same-category-same-post-format/#post-4012609)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
