Creating a List of Posts With the Same Categories as the Current One

If you're running a large blog, you'll need to find a way to help your readers find content that they're going to want to read, based on what they're currently reading or have just read.

One way to do this is by using one of the many plugins out there that identify related posts. These use various methods to identify other posts which have similar content to the current one, but sometimes they don't give you quite the results you want.

One logical way to identify posts like the one currently on display is by using categories, tags or taxonomy terms. If you could identify the posts in the same categories as the current one, you could then display a list of the most recent posts in the same categories, to help readers find related content.

In this tutorial I'll show you how to create a plugin to do just that. The plugin will give you a function which you then add to your single.php template file to show the list under the content of the current post.

What You'll Need

To follow this tutorial you'll need:

a development installation of WordPress

an existing theme with a single.php template file

a code editor

Setting Up the Plugin

Let's start by setting up the plugin. Create a new file for your plugin—I'm calling mine tutsplus-related-posts.php.

These arguments will find posts of the same post types as the current one, in the same categories, and exclude the current post. I've set it to output five posts, but you could always change this, or any of the other arguments such as post type if you've applied categories to more than one post type.

Adding the Function to Your Theme

The final step is to add the function to a template file in your theme. I've created a child theme for the Twenty Fourteen theme with a new single.php file, and I'm adding the function there, below the content. It can be added anywhere, as long as it's within the WordPress post loop.

In your template file, add the following:

tutsplus_related_posts();

You can see the result below:

Note: The demo site is a little confusing as tags and categories are listed at the end of each post. Our list doesn't use tags, just categories, of which each post in this site has one.

Alternatives: Tags and Taxonomy Terms

You can quite easily alter this function to work with tags or taxonomy terms instead.

To identify tags instead of categories, you would replace the get_the_category() function with get_the_tags(), and then replace the 'category__in' argument for the query with 'tag__in'.

To identify taxonomy terms, you'd use get_the_terms() in place of get_the_category(), adding the taxonomy slug as the second parameter. You'd then replace the 'category__in' argument with an argument using 'tax_query'.

You could also search for posts with the same terms in more than one taxonomy (including terms and categories), by creating two arrays and using two arguments in your query.

Summary

In this tutorial you've learned how to identify the categories the current post is in, add their IDs to an array, and then use that array to run a query displaying posts in the same category. This gives you more control than you get from most related posts plugins, and means you can define the arguments for your query so that the posts are displayed in exactly the way you want.

You can also adapt this technique to work with tags, taxonomy terms, or a combination of two or more.