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.

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.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

28 − = 20