Need some help with your project? Contact me

Related Content block using Taxonomy and Views in Drupal 7

I wrote a post about how you can create a Related Content block which displays content related to the rendered node based on the referenced entity in that particular node. So essentially, when you write an article, you can have a special reference field to reference other articles. And then you’d get those referenced articles displayed in that block. This was done with the Entity Reference module.

But there is another way to do this, using Taxonomy. This is for those cases in which you want to show related content more dynamically – content that can become related also after the publishing of the original node. Let me tell you what I mean.

You have, let’s say, Articles tagged with red or with blue in your taxonomy. And when viewing a node tagged with red, you want a block on the side (or wherever) to display other nodes tagged with red. You can accomplish this using Views.

Starting from the premise that you already have your content tagged, go and create a block View that displays only Articles (or whatever Content Type you have tagged). Here you can of course customize your View to display all kinds of information from the nodes.

What you need to do then is add 2 contextual filters: Content: Has taxonomy term ID and Content: Nid. Configure the first as following:

Under When the filter value is NOT available, select Provide Default Value of the type Taxonomy Term ID from URL. Then, check the box Load default filter from node page, that's good for related taxonomy blocks. Next, check Limit terms by vocabulary and under Vocabulary, check the vocabulary you want involved in this (the one which contains red and blue). Then, depending on your needs, select the appropriate choice under Multiple-value handling and apply your changes.

The second contextual filter has the purpose of excluding from the block the actual node you are currently viewing. Quickly configure it by choosing Content ID from URL under the Provide Default Value selection and under More, check the box Exclude. Then you can apply and finalize your view. If you save it and add the block to a page which shows a node tagged with red, the block will display other nodes that are tagged with red.

Hope this helps!


Great tutorial, thanks helped me a lot. Was going around in square circles trying to get this working!


No problem.
Glad I could help Brendan!

Is it possible to add a sort option so that content that has the most overlapping taxonomy terms will be displayed higher? Thanks!

I wish so hard this would work for me but I can't get this to work! I have 2 content types.

1-Banner CT (image, tag)
2-Article CT (title, body, tag)

The vocabulary that the tag is using is called Banner.

I want to create a block that will match the tags (from the Banner and Article CTs) and show the banner image in the header region (of the Article CT).

For some reason, using a contextual filter by "Content: Has taxonomy term ID" doesn't work at all for me. However, when I used the field name for the tag, it works...but only in the preview mode. It doesn't work on the actual pages. Instead, it appears that the filter is not validated.

Here's how I set up the contextual filter based on the field name:
When the filter is not available, provide default value: Taxonomy term ID from URL. Check: Load default filter from node page, that's good for related taxonomy blocks. Then I limit terms by vocabulary of Banner.

When the filter value is available or a default is provided, specify validation criteria. Validator: taxonomy term from vocabularies Banner. Filter value type: Term name converted to Term ID.

Then, "Action to take if filter value does not validate" Display all results for the specified field.

So, just for debugging purposes, I set it to display 10 images. If it works, the tags match up and it should only display 1 image. If not, it'll display 10. On the preview mode when I type in the tags, they correctly return in the preview box. However, on the pages, they display all images. So that means it's following the protocol for action to take if filter value does not validate.

But what does "filter value does not validate" even mean? Is the view block not able to grab the tag from the node it's on?

Please help. I'm using views 7.x-3.7 with Drupal core 7.26.

was very useful

Thanks for this post, it saved my sanity :)

I use this method all the time to show related content, e.g. related blog posts on a blog post page.

However, I just hit a problem trying to relate two different content types together. One used a Term Reference field for the taxonomy field, and the other used Entity Reference.

Turns out this method only works if you use the same field for both content types (or, maybe, both need Term Reference or Entity Reference - I didn't test that). Otherwise, the view just doesn't return anything. When I removed the Entity Reference field and used the existing Term Reference field instead, it worked.

So, I've finally tested this method with Entity Reference instead of Term reference. I found that simply replacing the Term Reference with an Entity Reference resulted in duplicate items in the results. However, I was able to reduce the duplicates by turning on Distinct in the Query settings in the view.

Many thanks for this clear and useful content.

Awesome. Thanks. Better than the similar by terms module!

thanks a lot that s great

Hi I was able to get related content to display based on the taxonomy on a particular node.

Example for taxonomy department When a node is tagged as tech and science all related similar tagged content shows up.

Next I want to add another field to this node which lets me further filter which tags to display for example only display tech blogs even though the node is tagged as tech and science is this possible?

Great Tutorial brother....thanks a lot!

Add new comment

You can post comments in Markdown and basic HTML tags.
For code blocks, wrap your code within '~~~'. For example:
$var = 'my variable';