Write a Better Related Posts Plugin for Custom Taxonomies

Related Posts Plugins are very popular as a method for WordPress users to display related content at the end of their posts, providing the reader with additional relevant content. Typically, related posts plugins use Post Tags to compare posts and find those that are related to the currently displayed post. The problem with this method is that it doesn’t work for any post type that doesn’t use Post Tags or that has custom taxonomies.

[box style=”notice”]You may download the plugin version of this tutorial for free here[/box]

So what we are going to do here is write a simple plugin that will greatly improve the relevancy of our related posts. We are also going to make it possible to utilize custom taxonomies as a comparison tool.

Because we are going to be using custom taxonomies as our comparison base, we will use the tax_query parameter of query_posts. This parameter is rather complex, so I’m not going to go into detail about it, but you can read my tutorial at WP Mods for a more detailed explanation.

1 – Setup The Plugin File

Our plugin is going to consist of just a single file with just one function inside of it. First, we need to define the plugin header:

A very important next step is to include a condition check that will cause the next couple of steps to only run if the wp_get_post_terms() function does not return empty.

1
2
3

if($tags){// the next couple of steps go in here}

We now have a variable called $tags that is holding all of the terms applied to the current post. The next step is to setup the arguments that we will pass to our get_posts() function, which will query the posts that are considered related.

Notice that I have setup three variables, one for each of the first three tags (or terms) applied to the post. These three tags are how we will consider a post to be related. In order to find the related posts, or others that have the same tags applied to them, I have setup a rather complex tax_query that makes the get_posts() function only look for posts that have these tags. As I mentioned above, the tax_query parameter is rather complicated, so please read the articles linked at the top for more info.

The next step is to setup the actual get_posts() call, which will display our related posts.

Notice how I have setup a temporary variable called $temp_post and set it equal to $post, and at the end of the foreach loop I have reset the $post variable back to its original state by setting it equal to $temp_post. This is very important because it ensures that we don’t cause any conflicts with the currently displayed post’s content.

Now, there is just one more step in the function writing process. Put this next line after the closing } of the if ($tags conditional check.

i would like to modify it in a way to use more than one taxonomy as related post filter…

Also as a suggestion, I would give a visitor an option to “checkbox” select which taxonomies want to be included in the related posts…

Lets say user is viewing the post with taxonomies (car type, brand,condition etc)
1. By default (show all or, all checked checkboxes) it should show all related posts to all taxonomies of current post (even better would be if this could also be specified in options, so we could select which taxonomies to use as related)

2. Visitor should be able to “uncheck” the taxonomy that they don’t want to use in related posts, so when the Post from taxonomies “Car – Ford – new” is viewed, user should be able to “uncheck” – condition taxonomy and get also used cars related to this post…

– Also using post type with custom taxonomies as filter for related posts would be great feature!

Hope you see it useful and it will be done.. if not, could you please point me where to start to achieve this?

Instead of hardcoding and expecting at least three array indices in the tags, maybe iterate through and use all of them like — (I also added post__not_in to omit the original from being related to itself)

Hi Pippin.
Great work here, thanks. One question: How can I split up line 14 to include my taxonomy (which is the only form of tagging posts I use on a custom post type) and ‘post_tag’, used on normal post types, to which my custom taxonomy also applies.
Complicated?
To be clear:
I have a custom taxonomy ‘hhie_artists’ which is used on ‘posts’ and CPT ‘hhie_artists’. ‘hhie_lyrics’ only uses ‘hhie_artists’ (no tags), but normal posts in category “Releases” use ‘hhie_artists’ AND tags. I would like to incorporate `$taxonomy = ‘post_tag’ && ‘hhie_artists´ if this is possible.
Thanks. Joe

I knew I’d worded that badly! What I would like is to incorporate a Custom Taxonomy and regular Post Tags as our related terms. This is because one of our CPTs only uses a custom taxonomy (no post tags), and our “normal Posts” use the custom taxonomy AND post tags. Does this make more sense?

Yes, that makes sense All you need to do is set the relation to “AND”:

'relation' => 'AND'

Tom

April 1, 2013

Hi Pippin,

I attempted to use this without the plugin. With the plugin and I am receiving this error: “Fatal error: Cannot use object of type WP_Error as array in”.

My goal is to show related tags from my custom taxonomy “intags” and for my custom post type “insights”. I added “intags” to the first line of your code and when I echoed out the function I added “insights” as per you example. Any thoughts?

But is there some way that I can get the terms from more than one taxonomy?
I have multiple taxonomies incorporated with posts, and I would like to show related custom post types (multiples). Its working fine with one taxonomy, but I need it with more than one.

Let me made an example:
My taxonomies are: t-web, t-world, t-brand, etc…
And my custom post types are: p-books, p-movies, p-games, etc…
I would like to show related products (p-books, p-movies…), from post terms (t-web,t-world), but I just can make it works with ONE taxonomie (t-web) search.

I tried: function pippin_related_post ($taxonomy = array (‘t-web’,’t-world’))
but didnt work (and I’m not very good with php). Can it be done??

Hey Pippin, nice plugin. Working perfectly! I want to add the term name above the related items, e.g. See more $first_tag_name items. I can get the id # by adding $first_tag there but have tried everything to get the name and am stumped. Any ideas?