How to Get Next or Previous Post in a Specific Tag?

get_adjacent_post(), which is used by all functions that return a (link to) the next or previous post, only has a $in_same_cat argument, which looks at the categories the post is in, not the tags.

You could hook into the get_[next|previous]_post_join to modify the join query for your call, but then it’s probably easier to copy the function, remove the category-specific code and replace it with tag-specific code. Or make it even more generic and submit it as a patch to WordPress 🙂

Leave a Comment