I am building a Next and Previous Post navigation for my 'Single post' pages, inside a plugin. What I am trying to do is get the Prev/Next post within the same category of a Custom Post Type.

So I have a CPT of 'Movies' with a taxonomy of 'Genre' with terms 'Drama, Thriller', Thriller is a child of the Drama term. I have multiple posts that have the categories 'Drama->Thriller'. If I am viewing one of these posts I would like to stay within the 'Thriller' category as I navigate through Prev/Next.

My code below attempts to achieve this but what I end up with for 'get_adjacent_post()' are empty strings.

Code breakdown:

  1. Hook into the 'wp_link_pages' filter to update navigation logic
  2. Find the $post current taxonomy
  3. Initially add all 'term_id' to $exclude_ids
  4. Find the first term that has a parent term
  5. Setup current taxonomy
  6. Remove $chosen term from $excluded_ids
  7. break out of the loop

I have hardcoded the $excluded_ids with '9' (9 = Drama) for now because I know this is the term id I need to exclude allowing only for 'Thriller' term id to be used to find Prev/Next posts.

I am open to any suggestions, either fixing this code or coming up with someone completely different to achieve this.

Appreciate your time ,

add_filter('wp_link_pages', array( $this, 'replace_paginated_posts'), 10, 2);

public function replace_paginated_posts( $output, $args ) {

global $wp_query, $post, $page, $numpages;

// Get all taxonomies for current post
$chosen = 0;
$tax = '';
$taxonomies = get_the_taxonomies($post);
foreach ($taxonomies as $key => $value) {
    // Get post terms
    $terms = wp_get_object_terms( $post->ID, $key );

    // Get all the term id's
    $excluded_ids = wp_list_pluck( $terms , 'term_id' );
    foreach ($terms as $key => $term) {
        // get the first term that has a parent and select its term
        if ($term->parent > 0) {
            $chosen = $term->term_id;
            $tax = $term->taxonomy;
        if ($chosen != 0) {
            // Remove $chosen term from $excluded_ids;
            $excluded_ids = array_diff($excluded_ids, array($chosen) );

if ($chosen == 0) {
    $excluded_ids = '';

$prev_post_link = get_permalink( get_adjacent_post(true, '9', true, $tax) );
$next_post_link = get_permalink( get_adjacent_post(true, '9', false, $tax) );

echo '<nav class="post-navigation">';

echo '<a href="'.$prev_post_link.'">Previous Post</a>';
echo '<a href="'.$next_post_link.'">Next Post</a>';

echo '</nav>';

$output = ob_get_contents();

return $output;

EDIT: I am receiving a SQL error

WordPress database error: []
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.post_date > '2016-04-04 23:21:34' AND p.post_type = 'movies' AND tt.taxonomy = 'genre' AND tt.term_id IN (12,13,14,10) AND p.ID NOT IN ( SELECT tr.object_id FROM wp_term_relationships tr LEFT JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (9) ) AND ( p.post_status = 'publish' OR p.post_status = 'private' ) ORDER BY p.post_date ASC LIMIT 1
  • It looks like there are many reasons why get_adjacent_post() may spit back an empty string. A couple troubleshooting questions: 1) if you add the $wpdb global variable to your function and a call to $wpdb->print_error(); after get_adjacent_post(), are any errors printed? 2) have you verified that $tax is 'genre' (or equivalent) in your calls to get_adjacent_post()? 3) You mentioned some movies have the terms "Drama->Thriller" - is that to say they are attributed with "Thriller" or both "Drama" and "Thriller"? – bosco Apr 5 '16 at 23:31
  • I edited my answer to show the SQL error I am receiving. Yes, I am getting the correct taxonomy of 'genre' and chosen of '10' in my loop. But I am still receive this SQL error even when I hard code the args into the function '$prev = get_adjacent_post(true, '9', true, 'genre');' – Benjamin Apr 6 '16 at 0:30
  • what is the code you use for post navigation in theme? – Jevuska Apr 6 '16 at 0:50
  • The plugin works by updating your theme anywhere you are using the template tag 'wp_link_pages', that is why the entry to my plugin logic starts with the 'wp_link_pages' filter hook. – Benjamin Apr 6 '16 at 0:56
  • if your plugin works later, what is the different with WordPress function the_post_navigation – Jevuska Apr 6 '16 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.