Blog

Sharing WordPress tips and tricks as I find them

Previous and Next Post in Same Taxonomy

» Download the Plugin | Support Forum

WordPress provides functions to add previous/next links to your posts. You can use the ‘in_same_cat’ parameter to limit the results to posts in the same category.

This is great for posts, but when you’re working with custom post types and taxonomies you lose this functionality, since ‘in_same_cat’ is limited to the taxonomy ‘category’. I’ve written a patch for WordPress that fixes this issue. If the patch is accepted it will be my first official contribution to WordPress core.

I’ve built the patch into a plugin so you can easily test and use it on your website.

  1. Download the plugin from WordPress.org and activate it
  2. Add the functions you’d like to test to your theme files.
  3. Let me know how you’re using it and if it works (or doesn’t) in the comments

I’m using the plugin right now at the bottom of my portfolio items (example). Here’s the code I’m using to render that box:

[php]
function be_single_project_navigation() {
// If the plugin is ever disabled, this disables the code
if( function_exists(‘be_previous_post_link’) ):
echo ‘<div class="project-navigation">’;
echo ‘<span class="title">Browse projects in the ‘.be_first_term(‘price-range’).’ range:</span>’;
be_previous_post_link(‘<span class="previous">%link</span>’, ‘« %title’, true, ”, ‘price-range’);
be_next_post_link(‘<span class="next">%link</span>’, ‘%title »’, true, ”, ‘price-range’);
echo ‘</div>’;
endif;
}

// A useful little function for getting the first tax term’s name.
function be_first_term($taxonomy) {
global $post;
$terms = get_the_terms( $post->ID, $taxonomy );
$show = true;
foreach ($terms as $term) {
if ($show) return $term->name;
$show = false;
}
}
[/php]

Here are the functions that have been modified to use taxonomies:

  • be_previous_post_link()
  • be_next_post_link()
  • be_adjacent_post_link()
  • be_get_previous_post()
  • be_get_next_post()
  • be_get_adjacent_post()
  • be_get_adjacent_post_rel_link()
  • be_adjacent_posts_rel_link()
  • be_next_post_rel_link()
  • be_prev_post_rel_link()
  • be_get_boundary_post()
  • be_get_boundary_post_rel_link()
  • be_start_post_rel_link()

Refer to the source of the plugin or WordPress Codex for documentation. They should work just like the original functions, except they now accept an additional parameter, $taxonomy, that’s set to ‘category’ by default.

If you have any issues or implementation questions, please leave a comment. Also share your successful implementations! I’m just as much interested in seeing how it’s used as I am in finding bugs.

I think this is a very useful addition and would love to see it in WordPress core. But even if it doesn’t make it, I’ll continue to maintain the plugin. I’m using it on my site and a client’s already.

7 Comments

  1. LIKE… very useful Bill!

  2. Hi Bill,

    Thank you very much for sharing this valuable code with us as we sometimes run into this problem when wanting to display the next/previous posts with the same custom taxonomy.

    The article is so nice that it made its way up and is now featured at our website, under the Crème de la Crème section:

    http://www.latestwp.com/2011/08/11/how-to-display-previous-and-next-post-with-the-same-taxonomy/

    All the best

  3. Thanks it’s useful…

  4. Damon Tribble says:

    Hi Bill,

    Thanks for your work on this plugin. I’m wondering if it will work for my situation or if there’s a workaround you can suggest.

    I have a taxonomy called “industry” on a portfolio site I’m building. Under that taxonomy, there are categories such as branding, packaging, identity, etc. I would like to make it so when someone views a post in the branding category (under the “industry” taxonomy) the previous and next links only show them other projects in the branding category. Is this possible to do with your plugin? So far, I have been able to get it to display all posts in the “industry” taxonomy but have been unable to restrict it to a certain category under that taxonomy. Thanks for any help you can offer.

    -Damon

    • Bill Erickson says:

      Yes, that’s exactly what this plugin should be used for. If you set ‘in_same_cat’ to true and ‘taxonomy’ to industry, the previous/next links will go to posts that are in the same industry (ex: one branding post will link to prev/next branding posts).

      Please send over the code you’re using in your theme so I can make sure it’s set up correctly. If it still doesn’t work I’d like to explore it a bit deeper to see if there’s any issues with the plugin.

  5. Damon Tribble says:

    Thanks for your quick response, Bill. Here’s a little more info:

    If I use the be_ code with the name of the taxonomy (i.e. “expertise”) it works fine:

    be_previous_post_link(‘%link’, ‘%title’, true, ”, ‘expertise’); be_next_post_link(‘%link’, ‘%title’, true, ”, ‘expertise’);

    My problem is that a project in this portfolio can have multiple terms under the “expertise” taxonomy (i.e. “branding, identity, interactive”). Thus, the code above will make it so the next/prev links point to all projects which share terms under the “expertise” taxonomy.

    I want to be able to limit the next/prev links to posts which use the same term under the expertise taxonomy (i.e. “branding”) like this:

    be_previous_post_link(‘%link’, ‘%title’, true, ”, ‘branding’); be_next_post_link(‘%link’, ‘%title’, true, ”, ‘branding’);

    But when I do this, I get the following error:

    Warning: implode() [function.implode]: Invalid arguments passed in C:\Users\Damon\Files\Web & Graphic Design\Projects\Seve7n Designs\Working site\trunk\wp-content\plugins\previous-and-next-post-in-same-taxonomy\previous-and-next-post-in-same-taxonomy.php on line 68

    Am I able to use your plugin to limit the next/prev links to a single term under a taxonomy?

    Thanks again for all your help!
    -Damon

  6. Thanks Bill, we will try your plug-in…