Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active May 21, 2016 02:20
Show Gist options
  • Save kjbrum/4409320737738e54f629 to your computer and use it in GitHub Desktop.
Save kjbrum/4409320737738e54f629 to your computer and use it in GitHub Desktop.
Create navigation links for a specific set of posts (post_type, meta_key, etc...).
<?php
/**
* Create navigation links for a specific set of posts (post_type, meta_key, etc...)
*
* @param string $post_type Optional post_type
* @param string $meta_key Optional meta_key
* @return array $nav_links The array of navigation links
*/
function wp_get_custom_post_nav( $post_type='post', $meta_key='' ) {
global $post;
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'order' => 'asc',
'orderby' => 'meta_value',
'meta_key' => $meta_key
);
if( ! $posts = get_posts( $args ) ) {
return false;
}
$nav_links = array();
$post_ids = array();
foreach( $posts as $p ) {
$post_ids[] += $p->ID;
}
$id = get_the_id();
$current = array_search($id, $post_ids);
if( ! empty( $class_ids[$current-1] ) ) {
$prev_id = $class_ids[$current-1];
}
if( ! empty( $class_ids[$current+1] ) ) {
$next_id = $class_ids[$current+1];
}
// Check for previous posts
if( ! empty( $prev_id ) ) {
$nav_links['prev'] = array(
'link' => get_permalink( $prev_id ),
'title' => get_the_title( $prev_id )
);
}
// Check for next posts
if( ! empty( $next_id ) ) {
$nav_links['next'] = array(
'link' => get_permalink( $next_id ),
'title' => get_the_title( $next_id )
);
}
return $nav_links;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment