Custom Post Type Navigation: Creating Prev/Next Links for Specific Category Posts

I’m working on a WordPress site with a custom post type called “Portfolio” and need help with navigation links.

Here’s my setup:

  • Custom post type: Portfolio
  • Custom taxonomy: portfolio_categories with URL structure portfolio/category-name
  • Some categories are parent categories with subcategories

I want to add previous/next navigation links in my single portfolio post template that only cycle through posts in the same category (not all portfolio posts).

I tried this code but it shows nothing:

<?php previous_post_link( '%link', 'Previous Work', TRUE ); ?>
<?php next_post_link( '%link', 'Next Work', TRUE ); ?>

When I remove the TRUE parameter, links appear but they include ALL portfolio posts instead of just the current category.

I think I need to:

  1. Get all posts from the current category
  2. Find the current post’s position in that list
  3. Show links to the posts before and after it

But I’m not sure how to write this code. Can someone help me create a solution that works with custom taxonomies?

Your issue is that previous_post_link() and next_post_link() don’t work with custom taxonomies by default. That TRUE parameter only excludes regular categories, not custom ones.

I ran into this exact problem on a client’s portfolio site last year. Here’s what fixed it:

$current_terms = wp_get_post_terms(get_the_ID(), 'portfolio_categories');
if (!empty($current_terms)) {
    $term_ids = wp_list_pluck($current_terms, 'term_id');
    
    $prev_post = get_previous_post(true, '', 'portfolio_categories');
    $next_post = get_next_post(true, '', 'portfolio_categories');
    
    if ($prev_post) {
        echo '<a href="' + get_permalink($prev_post->ID) + '">Previous Work</a>';
    }
    if ($next_post) {
        echo '<a href="' + get_permalink($next_post->ID) + '">Next Work</a>';
    }
}

Switch to get_previous_post() and get_next_post() instead of the link functions, then pass your custom taxonomy as the third parameter. This’ll keep navigation within the same portfolio category.

WordPress’s built-in functions are a pain with custom taxonomies. Had the same issue building a real estate site with property types. Here’s what actually worked:

Create a custom function that manually queries adjacent posts within the same taxonomy term. First, grab your current post’s taxonomy terms. Then use WP_Query to pull posts from that category, ordered by date or menu order. Find your current post’s position in that array and grab the previous/next items.

I also cached the results - running these queries on every page view kills performance. Just make sure you’re filtering by the exact taxonomy slug and handle cases where posts might be in multiple categories.

just use get_adjacent_post() with your taxonomy name. for next: get_adjacent_post(false, '', true, 'portfolio_categories') and for previous set the third param to false. way simpler than custom queries and no caching headaches.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.