Profile picture for user admin
Daniel Sipos
03 Oct 2012

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!

Profile picture for user admin

Daniel Sipos

CEO @ Web Omelette

Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.

Contact us

Comments

Brendan 03 Apr 2013 09:06

Thanks

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

Cheers
Brendan

Daniel Sipos 03 Apr 2013 10:38

In reply to by Brendan (not verified)

No problem.

No problem.
Glad I could help Brendan!

Pieter-Jan 17 May 2013 15:07

Sort

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

Valery 29 Jan 2014 23:52

I wish so hard this would

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.

millionleaves 29 Jun 2017 05:11

If relating two different content types, use the same field

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.

millionleaves 08 Sep 2017 10:31

In reply to by millionleaves (not verified)

The same solution with Entity Reference

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.

Matt 14 Sep 2017 19:44

mattwmc2001@gmail.com

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

Jack 26 Sep 2018 23:36

Related Content filter by field

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?

Add new comment