How to get the next post inside the current taxonomy? If your using the standard wordpress post, you can use next_post_link() and even specify to use the category but for a custom post type this solution doesn’t work, so I wrote the following lines.
Add the code below to your single-.php at the position where you want to show your NEXT link.
In this example the custom taxonomy is “sphere” and the custom post type is “project”.
I wanted the post to wrap around, so if there is no next one then use the first one.
The posts are ordered by menu order.
<?php //remember the id and menu_order from current post $id=get_the_ID(); global $haet_post_order; $haet_post_order=$post->menu_order; //There is only one sphere per project in this example $custom_terms = get_the_terms($id, 'sphere'); foreach ($custom_terms AS $term) { $sphere=$term->slug; } function haet_filter_next_post( $where = '' ) { global $haet_post_order; $where .= ' AND menu_order>'.$haet_post_order; return $where; } add_filter( 'posts_where', 'haet_filter_next_post' ); $loop = new WP_Query( array( 'post_type' => 'project', 'posts_per_page' => 1,'orderby' => 'menu_order','order' => 'ASC','sphere'=>$sphere ) ); remove_filter( 'posts_where', 'haet_filter_next_post' ); //if there is no next post use the first one if( !$loop->have_posts() ){ $loop = new WP_Query( array( 'post_type' => 'project', 'posts_per_page' => 1,'orderby' => 'menu_order','order' => 'ASC','sphere'=>$sphere ) ); } while ( $loop->have_posts() ) : $loop->the_post(); // place you code here ?> <?php endwhile; ?>
Do you have any improvements? Or a completely different approach? Please add to the comments.