Monday, July 9, 2012

List Related/Similar Nodes using Views 3

Want to show a list of related or similar nodes in a block on the current node's page?   All you need is Views.

These instructions will help you create a View of related nodes (based on shared taxonomy terms) in order of relevance.  Nodes that share more terms with the current node are shown at the top of the list.  For example, if you have a node IPhone 4S node with terms Gadget, Apple, and Phone, you can get similar nodes in the following order:

IPhone 3 (Apple, Gadget, Phone)
IPad (Apple, Gadget, Tablet)
Mac (Apple, Computer)
Bramley (Apple, Fruit, Tree)

To create the View:
  1. Create a block view
  2. Add Contextual filter -> Content: Nid -> Provide default value -> Content ID from URL
  3. Add Relationship -> Content: Taxonomy terms on node -> specify the appropriate vocabulary
  4. Add Relationship -> Taxonomy term: Content using vocabulary as specified above -> Check off Require this relationship
  5. Turn on Views aggregation
  6. Assuming you are listing title only, edit the title field to use the Relationship you set up in #4 above.
  7. Add new sort criteria of Content: Nid. In aggregation settings, select Count. Use relationship from #4 and sort descending
  8. Add Contextual filter -> Content: Nid -> Use relationship from #4 -> Provide default value - Content ID from URL -> Scroll down and expand "More" then check "Exclude" to remove current node from the view
Created with Drupal 7, Views 7.x-3.3 -- Click to enlarge

Credits: From my answer on Stackoverflow

No comments:

Post a Comment