I have posts which assigned 1 of 2 different categories.

On the single post view i want to show a next and previous link of posts with the SAME category.

This should be straightforward, i've tried using get_previous_post etc but i cannot exclude posts assigned to the other category.

I've tried

$prevPost = get_adjacent_post(TRUE,'129',TRUE);

where 129 is the id of the category i want to exclude, but this does not work.

The posts are assigned categories correctly i.e. they are not assigned both categories.

The query is as follows

SELECT p.ID FROM wp_posts AS p INNER JOIN wp_term_relationships AS tr ON p.ID = tr.object_id INNER JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE p.menu_order < '2' AND p.post_type = 'post' AND p.post_status = 'publish' ORDER BY p.menu_order DESC LIMIT 1

As you can see it doesn't reference the category to exclude.

Is this a wordpress bug or am i missing something here?



1 Answer 1


Use the_post_navigation( $args ); to display next and previous links in your template for a single post. To restrict links to the same category, your $args array, in its simplest form, should be:

$args = array(
    'in_same_term' => true,

Other parameters are optional. Get their list here.

  • This doesn't work for me, it still pulls through a post with a different category. I'm setting 'in_same_term' to true in my previous example but it doesn't seem to have any effect. Commented May 3, 2017 at 15:10
  • Try to add 'taxonomy' => 'category', to your $args array. I didn't do it because 'category' is supposed to be a default. Commented May 3, 2017 at 20:43
  • No joy unfortunately. Still not working as expected. There's an issue with get_adjacent_post i think, it constructs $where based on the term you've excluded here github.com/WordPress/WordPress/blob/master/wp-includes/… , then it changes $where here so that it doesnt exclude that term github.com/WordPress/WordPress/blob/master/wp-includes/… Commented May 4, 2017 at 9:50
  • Do not use $prevPost = get_adjacent_post(TRUE,'129',TRUE); and use standard query. Display all posts. When you select a single post, your 'next' and 'previous' links should select posts within SAME category. Commented May 4, 2017 at 11:25

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.