Category Based Previous & Next Links on Custom Post Types for WordPress

Working on a client project in WordPress recently, I came across something I’d not thought of before…

Category based Previous and Next links on Custom Post Types.

I’d heard of category based links for standard blog post but I’d never actually seen it used. After a bit of Googling and looking back through previous projects I managed to find a solution.

The Use Case

  1. I have a custom post type called “Products”
  2. I have a custom taxonomy called “Product Categories”
  3. I have a bunch of products that I want to link to via product category
  4. Whilst in this individual product page I want to find out my current custom post type category and display previous and next links to allow me to navigate but only within my current product category

The Code

Here is the code I used to grab the current category and then get the posts based on that category…

<?php

// Get terms for post
$terms = get_the_terms( $post->ID , 'productcat' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
$termSlug = $term->slug;
// Get rid of the other data stored in the object, since it's not needed
unset($term);
} } ?>

<?php

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

echo $term->slug; // will show the slug

// get_posts in same custom taxonomy
$postlist_args = array(
'posts_per_page' => -1,
'orderby' => 'menu_order title',
'order' => 'ASC',
'post_type' => 'product', // this can be your post type
'productcat' => $termSlug // get slug of product category from above - change productcat for your taxonomy slug
);
$postlist = get_posts( $postlist_args );

// get ids of posts retrieved from get_posts
$ids = array();
foreach ($postlist as $thepost) {
$ids[] = $thepost->ID;
}

// get and echo previous and next post in the same taxonomy
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];

?>

Then the following to display the above…

<?php

if ( !empty($previd) ) {
echo '<a rel="prev" href="' . get_permalink($previd). '">previous</a>';
}

if ( !empty($nextid) ) {
echo '<a rel="next" href="' . get_permalink($nextid). '">next</a>';
}

?>

16 thoughts on “Category Based Previous & Next Links on Custom Post Types for WordPress”

  1. hey Chris, thanks for this – exactly what I needed. One question – doesn’t it significantly slow down your page load? On my site its taking a good few seconds to run the code. How do you get around this?

    1. Hi Ant, I’d not noticed any difference in page load at all and that’s on a really low spec cloud hosting account (dev site) and on a dedicated VPS (live site)

  2. Cheers for this, finally a solution that actually works! I also used it for general CPT pagination by just removing the taxonomy elements and it seems to work fine

    Thanks

  3. Hey Chris, sorry, for asking. I should enter the first code into my functions.php? Is there a possibility to show the title of the prev/next post? Just like %title when using WordPress own next_post_link? Cheers

    1. Hey Chris! Thank you for these codes. I am a newbie and just wanted to make sure I know where to put those two different codes exactly at. Do I add the first portion in functions.php and the second one in index.php ? Thank you.

      1. Hi James, no it would all go wherever you call the navigation.

        So with this example, the code goes in a file called “_part-nav-product.php” and is reference from the “single-product.php” file as a get_template_part()

        1. Hey. I just found this post today. I’m pretty new to this and I’m trying out my first Child Theme. In order to get this to work, do I need to create a new document in my Child Theme called “_part-nav-product.php” or do I need to find out where Divi calls navigation and name my Child Theme navigation file whatever that is?

  4. Just used this to get prev/next navigation on Portfolio Posts Type pages with custom menu order functionality added. All solutions before did not stay in portfolio post type categories (terms). This was perfect, thank you so much!!!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top