Using Views Contextual Filters to Create a Related Content Block With Drupal 7

I recently built my new photo portfolio site High Rock Photo and on single image gallery pages (a Content Type I created), I wanted to show "related" images that were in the same category. That is to say images that were tagged with the same taxonomy term. In my case, I have categories assigned to images such as "Architecture", "Maritime", "Nature", etc... Each image is only assigned to one category. On each image page, I wanted to automatically show a "related" content block of similar images. I knew the ideal way to do this would be with Views but it ended up being a little tricky. In the end, Views Contextual Filters was the solution. 

Related Images block using Views Contextual Filters

If you are not familiar with Views Contextual Filters for Drupal 7, it was known as Views Arguments in Drupal 6 and then got renamed for Drupal 7. Essentially, Contextual Filters intelligently filters content to reduce the result set in various ways and is pretty powerful. You can also have more than one filter set on a View. By using this method, it makes your View scalable and one View can be used on many pages on your site rather than having to create many views, one for each page / block. 

To begin, set up a basic View with a Block that shows the Node Title which is usually added by default in Views 3 for Drupal 7. I also added an Image from the content type I am using for this View. In my case the content type is called "Image Page" and I am using the Node Title and an Image in my View. You can configure the display any way that suits your view. In the end, I just wanted the images to show so I removed the Node Title from the View but I always like to create a View using the Node Title to begin with just to make sure you are getting the right data. I also made the image clickable to it's content page by setting "Link image" to "Content". 

Next, under the "Advanced" area of the Views UI admin, expand that and you will see "Contextual Filters". We are going to add two here, the first one will be "Content: Nid". Under "When the filter value is NOT available", select Provide default value and choose in the select list, Content ID from URL. What this does is make it so your actual node that you are currently in does not show up in the related content block. One more setting not shown in the screen capture would be to set "exclude" in the more section at the bottom. 

Contextual Filter - 'Content: Nid'

Now add a new filter and this time use: Content: Has taxonomy term ID". Under "When the filter value is NOT available", choose "Provide default value" and choose type of "Taxonomy term ID from URL". Next choose "Load default filter from node page, that's good for related taxonomy blocks" and that will then toggle to make visible any Taxonomy Vocabulary that could be relevant, in my case it's "Image Category". Note, that within the Views UI, auto preview will not work once you have assigned Contextual Filters unless you enter in the arguments. 

Contextual Filter - 'Content: Has taxonomy term ID'

Your View should be complete now, save it an assign it to the relevant content pages.  In my case, I assigned the View Block I created to my "Image Page" Content Type pages. You can do this either using the Context Module or using the traditional method of the blocks admin page. You can see a working version of the view here.

Tags: 
  • Views
  • Taxonomy
  • Contextual Filters
  • Drupal Planet

Comments

Thanks for this!
The screenshots make it really easy to understand and copy .
Just thought I'd take this opportunity to mention a related D7 module that also takes advantage of View's contextual filters, albeit for a slightly different purpose: Views Global Filter. The project page describes its uses.

I find that this method never quite gets a good picture of 'related content'. An event 2yrs old is probably not interesting, and what about something that doesn't have the exact same set of taxonomy terms, but is actually very much related. And so I find that using ApacheSolr module's built-in 'more like this' block does a much better job than creating a manual query - with Views module or otherwise.

This was VERY helpful for me, but I found two steps were missing.

1) On the Content: NID filter, you need to check "Exclude" in the More section. That removes the current node from the items.
2) On the taxonomy term filter, it's very useful if you have multiple related terms (tags) to click both "Reduce duplicates" and "Allow multiple values" in the More section.

Thanks again for the tips.

Thanks so much, this is exactly what I needed to know. :)

In my case, the block is a view of publications (via the biblio content type). I am trying to show publications related to specific research projects. So the basic page is a content type of "research highlights," and a want the block loaded on that page listing the related publications. I have a taxonomy called "pub_tags" that is used in the biblio content and in the research content. So basically, I need the block view to be able to filter based on the pub_tag in the research highlight page.

I spent hours and hours for excatly this !!! Thax a lot again :)

No way to make it work...

I relate nodes to terms with different fields (ex: pages have a field_page_tags and related contents have a field called field_rel_tags); could this be the problem?

this tutorial plus the papagrande´s addition, works perfect, thanks.

great resource here

Add new comment