Site icon StartFunction

How to display related posts using custom taxonomies

A good technique to help minimize bounce rates in a website is to offer content related to the one that is currently being consumed by the user. We’re going to learn here how to achieve this in WordPress using custom taxonomies, but you can use post tags or categories with the same code.

Custom taxonomies

What we are going to do in this tutorial, is to display other posts related to the current post filtering them by its terms applied in a custom taxonomy from WordPress. These taxonomies allow us to group things under different terms. In this case, we will use a taxonomy named color, and the terms to group by will be colors: blue, red, green, orange, pink, etc.

Then, when the visitor is reading a post including terms from color taxonomy like orange or beige, we will query our posts to see if there are other entries containing the same terms and will display these posts or entries in a block with some title like “Related posts by color”.

Code to display related posts

Let’s check the full code first. One place that you could use this is in the single.php template of your WordPress theme. The single.php template is used by WordPress to display individual entries, thus making it perfect to show related content.

<div id="related-posts">
<h3>Related posts by color</h3>
global $post;
$terms = get_the_terms( $post->ID , ‘color’, ‘string’);
$do_not_duplicate[] = $post->ID;

foreach ($terms as $term) {
query_posts( array(
‘color’ => $term->slug,
‘showposts’ => 4,
‘caller_get_posts’ => 1,
‘post__not_in’ => $do_not_duplicate ) );
while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
<div id="post-<?php the_ID(); ?>" class="related-post">
<?php the_title();?>
<div class="ilc-excerpt">
<?php the_excerpt(); ?>
<?php endwhile; wp_reset_query();

We will first instantiate the global variable $post, to access the current post data. Then we collect the terms in the entry with get_the_terms. We’re also going to initialize an array with the ID of the current post, so that we won’t repeat it again in the related posts (since it has the same terms it would appear in the new query).

The code now checks if there are any terms in the entry, checking if $terms is empty. If there are terms, we enter into a foreach loop iterating through each term. Now we’re ready to query the posts, comparing their terms in the color taxonomy to see if any of them match those of the current post. We will display only 4 related posts but feel free to change this number. Finally, we use the $do_not_duplicate defined earlier to indicate that we don’t want entries that array.

If the query was successful, we’ll have some entries to show and hence, we will enter The Loop. In this simple example we’re only showing the title and the excerpt but you can use any other function tag that work within The Loop. You can see a working sample of this code. It’s very similar, but it uses other calls to the_post_thumbnail and other post information such as the category.

Don’t think this code restricts you to use it only with custom taxonomies, you can also use it with post_tag or category, WordPress’ default taxonomies for tags and categories. Just change the taxonomy name from color to any of the two and it will work with the tags that you assign to your post or the categories defined for the post.

Exit mobile version