I have a custom post type "cases" and a custom taxonomy "before-after-gallery". On my single-case.php my prev and next post links are jumping from one category to another.

    <?php next_post_link('%link', 'Next Case', TRUE , null , 'before-after-gallery');?>

I am telling WP to stay in the same term and not sure why its jumping. I have spent hours trying multiple solutions but no matter what I can not get the post to stay in the same category.

This only happens on the cases in which have multiple terms on each case. I have tried to omit the other terms but I can not find a way to say "this is my current term" and get the id of that term and omit others.

Running a standard wp loop

New contributor
Lear2000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • This answer might help you: stackoverflow.com/a/30171620/107244 – Michelle Mar 14 at 19:53
  • $GLOBALS['just_this_category'] = 2; this cant work, due to the fact if i were to go to the next post the cat id would change , I dumped out the vars for the next post and the top post was the non correct category id. – Lear2000 Mar 15 at 3:51
  • What do you consider the 'current term' for a single post? is it the first-listed term? something else? if it's first-listed, you could use something like wp_get_post_terms()[0] to retrieve the first result and use that as the current term, and exclude all others in the array. – Michelle Mar 16 at 4:20
  • That is correct except its not the 1st in the array, its the second, so I go from catNameOne click next.. then the term[0] is not catNameOne its catNameThree, so I would not be able to tell which catNameXXX I am in once I leave the current Cat. I must be missing something, i am single- .php and the prev and next are in the loop. – Lear2000 yesterday
  • I don't think you're missing anything - this is expected behavior based on how the in_same_term parameter is described on codex.wordpress.org/Function_Reference/next_post_link. You'd probably need to write a custom wp_query filtering posts by a specific term, then add post-navigation to that. This answer goes into a ton of detail that might help: wordpress.stackexchange.com/a/120408/16 – Michelle 16 hours ago

Your Answer

Lear2000 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.