Creating list of similar nodes in Drupal 7/Views 3

21,655

Solution 1

  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 (NOTE: the arrow in the screenshot below is pointing to the wrong spot and should be pointing to 2 rows below where it is)
  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

** See comment by @garret-albright below for testing this in the Views UI preview. **

Related node view

Solution 2

guess what - there are modules for that too :D

i can recommend similar by terms giving you that view without configuration hassle.

Share:
21,655
Stanislav Kniazev
Author by

Stanislav Kniazev

Updated on August 05, 2020

Comments

  • Stanislav Kniazev
    Stanislav Kniazev over 3 years

    Copy from drupal.stackexchange.com hoping to get more answers here:

    I face a rather common problem of displaying a list of nodes similar to current one. Similar node is defined as one having at least one taxonomy term that current node has. There are few solutions available as modules, however the modules I've found (specifically Similar By Terms) are in beta stage (as in "not working properly") for Drupal 7.

    I've also found a couple of solutions based on Views and contextual filters (such as http://scito.ch/content/show-related-nodes-block-based-taxonomy-terms-views-3-and-drupal-7) or relations. I quite like this way as it saves me installing extra modules and with Views being actively developed and supported I'd prefer to use them rather than any custom solution. The problem I can't get around is how to sort "similar nodes" by the number of terms they share. For example, if I have "IPhone 4S" Node with Terms "Gadget", "Apple", and "Phone", I want similar nodes in the following order:

    1. IPhone 3 (Apple, Gadget, Phone)
    2. IPad (Apple, Gadget, Tablet)
    3. Mac (Apple, Computer)
    4. Bramley (Apple, Fruit, Tree)

    How can I sort nodes in this view by the number of terms they share?

    This question is about building a view, please do not suggest module or custom php-based solutions. Thank you.

  • Stanislav Kniazev
    Stanislav Kniazev almost 12 years
    Thank you for an exemplary answer! Exclusion of the node itself is just another simple step. Could you please edit your answer to include the following: 7. ... +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 so this tutorial is complete and the bounty is yours!
  • Alexander Kim
    Alexander Kim over 10 years
    Cannot apply relationships from #4 to a title field. There's just no such option! Probably this method isn't working for Views 3.7
  • nmc
    nmc about 10 years
    @Heihachi I just tested this with the latest version of Views and it is currently working for me.
  • Cory Fischer
    Cory Fischer about 10 years
    You, sir, saved me hours of headaches. (although 5 on your image is pointing to the wrong spot—confused me for a minute).
  • pykone
    pykone over 9 years
    I'm having trouble replicating this. After adding the second contextual filter in step 8, Views does the "no query was run" thing, but I'm not seeing any error messages or notices. Yes, I was sure to set the relationship on it. (Also, I'm unclear what step 7 is accomplishing - why would we want to sort on this?)
  • nmc
    nmc over 9 years
    @GarrettAlbright Sorting in step 7 is a sort by number of taxonnomy term matches. This ensures that the nodes with most taxonomy term matches are at the top (ie. the ones most similar/related to the current node). I haven't used this View in a long time so not too sure why you're running into troubles. Consider asking a separate question for that problem.
  • pykone
    pykone over 9 years
    @nmc Oh, I see. I didn't need that sort of functionality in my case, but I see where the OP is asking for it. I guess that means I can also not bother using aggregation. At any rate, I'll keep tinkering with it. Thanks.
  • pykone
    pykone over 9 years
    @nmc Okay, I figured it out - for some reason the Views UI preview wouldn't work if I gave it a single NID argument like "123" - stepping through my debugger, it seemed to unset the argument after the first relationship used it, then fail when the second relationship had no arguments to use. Doubling the arguments in the Views UI argument field - "123/123" gave me the expected behavior, as did just putting the block on the node page. All is well.
  • seeming.amusing
    seeming.amusing over 8 years
    @garret-albright Your comment saved me from banging my head on the wall wondering why I couldn't see anything in the views admin. It'd be great if this tidbit is mentioned in the answer itself.
  • gerl
    gerl over 8 years
    I've tried this but its not working. Does it matter if I dont have any IDs in my url? Or am I taking this literally? My url pattern is [node:field_tags]/[node:title] so I'm not using any digits (content iDs) at all.
  • cptstarling
    cptstarling over 8 years
    This is how it's done when you want to display related nodes of a different content type, but with common terms: drupal.stackexchange.com/questions/180086/…
  • Patrice
    Patrice over 8 years
    This didnt work for me in D8. instead I folowed this tutorial that worked immédiatly : scito.ch/content/…