I have a blog with two categories of posts that I don't want 'mixing' with the rest of the posts for purposes of the single post navigation. I've been able to modify the next/previous for all the other posts but I'm trying to modify for these two categories.

Using the parameters of next_post_link I can set in_same_term to true. The trouble is many of these posts have multiple categories assigned. What I'm trying to do is restrict the previous / next links to only the one cateogry. So, in this case the category I'm trying to stay in is ID 152. If the current post is in 152, 12, 19 I want the next and previous link to be to a post in category 152, not 12 or 19. Limiting the term (5th parameter) to 152 didn't work. Any ideas?

Thanks Ben

1 Answer 1


You can use the combination of in_same_term and excluded_terms. First of all, you have the ID of the category you want to display post from. Next, get all the id of the category of the post by using wp_get_post_terms

You can iterate through the returned terms and build an array of terms that you want to exclude. That should do the trick.

  • I don't think that will work. I want to display any post with category 152 as one of it's categories. It could have 152 and 12 or 152 and 17. If it does I don't want to exclude cats 12 & 17, but if I don't exclude them it will display posts with only 12 or 17. Am I missing something? I believe I understand the logic but I'm not positive.
    – panbo
    Mar 24, 2018 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.