Skip to content

Instantly share code, notes, and snippets.

@JoshuaDoshua
Created March 21, 2016 16:34
Show Gist options
  • Save JoshuaDoshua/a791dd90efa4e9dda63c to your computer and use it in GitHub Desktop.
Save JoshuaDoshua/a791dd90efa4e9dda63c to your computer and use it in GitHub Desktop.
/**
* get previous/next post
* default WP postnav is limited
* modified from link below to handle post_type and cats differently
*
* @param string $direction [which direction to go]
* @param string $postTypes [what post types should w]
* @return [type] [description]
*/
function my_adjacent_post($direction = 'prev', $inSameTax = false, $tax = null, $postTypes = null, $reverseTax = false)
{
global $post;
if(!$postTypes) $postTypes = [$post->post_type];
$op = ($direction == 'prev' ? 'before' : 'after');
$order = ($direction == 'prev' ? 'DESC' : 'ASC');
$args = array(
'post_type' => $postTypes,
'showposts' => 1,
'date_query' => [$op => $post->post_date],
'orderby' => 'post_date',
'order' => $order
);
if ($inSameTax):
$topCat = power_top_cat($post->ID, $tax, $reverseTax);
$args['tax_query'] = array([
'taxonomy' => $tax,
'field' => 'id',
'terms' => $topCat->term_id
]);
endif; // limit to taxonomy
$posts = get_posts($args);
return empty($posts) ? false : $posts[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment