Are you trying to add a block of related content to a node page in Drupal? Do you want to display other nodes which share terms with the currently displayed node?
Have you been getting frustrated searching D.O for an answer? Are you weeding through Stack Overflow or Drupal Exchange posts for this common use case?
I'm going to help you with Drupal related content using the Views module. If you just want to the highly actionable parts (building the View and contextual filters), use the video walkthrough. However, I strongly suggest reviewing the use case and logic below, first. This might even help you when you're working through requirements on your other projects.
Drupal Related Content
There's many ways to attempt to identify Drupal related content. You could even use an external service that incorporates "relevance" through an algorithm by way of Apache Solr for example. Or, maybe you want to show related content by word frequency among two nodes. You could even directly identify related content with a one-to-one relationship with an entity reference. Today, we're going to work through a bit simpler, more common method of deriving and displaying related content (side note: if you're actually looking for one of those more complex routes, we do offer Drupal consulting!). It's a frequent enough use case that I find myself repeating the steps for different projects, yet there's enough steps that I may need a refresher by Googling around a bit. Yet, every time, I see folks stumbling through this or apparently not able to find the way to do it. Let's do it right. Here's what you'll need and what I'm using:
Requirements to display Related Content by Taxonomy Terms:
- Drupal Core 7.36+
- Views module 3.10+
Basic stuff! Now, let's talk about the use case we're working against. You may need only a subset of the apparent functionality, and that's okay. Derive what you need from the fuller explanation.
Related Nodes by Terms Use Case
First, we should think about our content. Let's say we have seven nodes of a certain content type, titled:
- Pogo Stick
Now, what about our classifications system? Let's say we add a taxonomy as a term reference field on the content type. It has terms:
We'll be logical and go ahead and add the right terms to each of our nodes as follows:
- Pogo Stick
With this data, we want to display related content based on the terms that are applied to each node. Let's use a few examples and plain english to flesh out requirements:
What Other Nodes Should be Related and Shown for:
Any idea? Well, node Bike has term Wheeled. Let's just show the nodes with that term. These nodes have term Wheeled and should be shown: Car, Bus, Train, Skateboard, Bike. That's pretty simple, but there's already a bit more going on you may not realize right away. For example, Bike, should not be shown even though it does have term Wheeled. Why? The user doesn't want to see the node they're already looking at. We'll want to add a requirement based on this:
1) The related content should not show the current node.
Node Car has two terms: Wheeled, Motored. Now we'll need to decide what to do with two terms. Do we show only the nodes that have both terms or do we show any node that has either term? For our use case, we're going to go with the more difficult example:
2) The related content should show nodes matching any term.
Ok, so let's take each term and query our node list, Wheeled first:
Car (eliminated by requirement 1), Bike, Bus, Train, Skateboard. Now, let's query our second term, Motored: Car, Boat, Bus, Train. So, we have our output when put together: Bike, Bus, Train, Skateboard, Boat, Bus, Train. Wait! We have duplicates! The user doesn't want to see the same thing twice. Time for a new requirement:
3) The related content should not contain any duplicate nodes.
Where does that leave us? Our requirements for our related content by term in Drupal functionality are that the related content should:
- Not show the current node
- Show nodes matching any term
- Not contain any duplicate nodes
Is that what you want to build? Let's do it! Got your nodes, taxonomy, and terms together? Excellent! I bet you want to walkthrough the View. This is a great time to go ahead and hit the video on this post.
Review Creating a Drupal related content Block with Views
Make sure you've created your nodes. Then, add a taxonomy and terms. Then, create a View->Block. Let's walkthrough the other major points in screenshots:
Add a Contextual Filter->Has Taxonomy Term ID
Remove Duplicates with the Contextual Filter
Add another Contextual Filter but with Content: NID
Remove the Current Node with the Contextual Filter
Need more help? If you're seeing Pogo Sticks, something broke! Feel free to ping us on Twitter, subscribe on YouTube for more, or follow us on social media. We have a lot more planned. Think this guide is useful? Don't forget to share!