30

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
  • 5,386
  • 3
  • 35
  • 44

5 Answers5

82
  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

nmc
  • 8,724
  • 5
  • 36
  • 68
  • 4
    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! – Stanislav Kniazev Jul 07 '12 at 15:41
  • 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 – Alexander Kim Oct 05 '13 at 10:24
  • @Heihachi I just tested this with the latest version of Views and it is currently working for me. – nmc Jan 29 '14 at 21:06
  • You, sir, saved me hours of headaches. (although 5 on your image is pointing to the wrong spot—confused me for a minute). – Cory Fischer Apr 06 '14 at 04:28
  • 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?) – Garrett Albright Sep 15 '14 at 03:23
  • @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. – nmc Sep 15 '14 at 13:03
  • @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. – Garrett Albright Sep 15 '14 at 15:37
  • 2
    @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. – Garrett Albright Sep 17 '14 at 04:46
  • @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. – seeming.amusing Aug 01 '15 at 11:39
  • 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. – gerl Oct 21 '15 at 19:24
  • This is how it's done when you want to display related nodes of a different content type, but with common terms: http://drupal.stackexchange.com/questions/180086/show-nodes-of-other-content-type-that-share-a-common-term-with-depth – cptstarling Nov 06 '15 at 13:47
  • This didnt work for me in D8. instead I folowed this tutorial that worked immédiatly : http://scito.ch/content/show-related-nodes-block-based-taxonomy-terms-views-3-and-drupal-7 – Patrice Jan 07 '16 at 11:09
2

guess what - there are modules for that too :D

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

rémy
  • 1,026
  • 13
  • 18
0

When you are configuring complex views like this one, there are some configuration tips that may help you to understand what is really going on each step:

In Structure >> Views >> Settings >> Basic:

  • Check 'Show advanced help warning' (important: requires Advanced Help module installed)
  • Check 'Automatically update preview on changes'
  • Check 'Show information and statistics about the view during live preview'
  • Check 'Show the SQL query'
  • and finally, Save configuration

Now you can preview the results from each configuration step, the SQL query associated and the fields and tables involved, so you will find easier to understand how filters and aggregations work.

Hope it helps.

Interdruper
  • 221
  • 1
  • 4
0

This is an alternative solution, related to this question. However it might not apply to every case, but if you can use it it's simpler (and safer than aggregation in my opinion).

I am going to use panels. Once you create a panel to show your entity (node or anything else) you can add multiple views and show them beside the content. one of these views is going to be our related content.
Thus panels is the only limitation. Maybe it's possible to use mini-panels and represent it as a block, I haven't tried myself.

Prerequisites

  • Views (d'oh!)
  • Panels
  • Views Content Pane: no need to download, It is in ctools module downloaded with views.
  • Page manager: Same as Views Content Pane (Optional / Highly Recommended).


The view

  1. Create your view as you normally would, but do not create it as a block or page. Uncheck both.

  2. In views configuration page create a relation to taxonomy term on the entity.

  3. Create a contextual filter to filter: Taxonomy term: Term ID.

  4. Create another contextual filter: Entity ID or Content: Nid if it's a node.

    • While creating this one, at the bottom there is a collapsed fieldset which has the option: Exclude. Check it as marked.

  5. Now by clicking Add create a new display of type Content pane.

  6. In the display configuration area (named: Pane settings) open Argument input. You should have 2 options:

    • Content: Nid source : Set it's source as From context. Set it's Required context as Content ID (if you are not using node it can be ID.
    • Taxonomy term: Term ID source : Set it's source as From context. Set it's Required context as Term ID.

Congrats! You are done creating your view. It's time for a panel (I wont explain much about creating a panel as it is beyond the scope of this Q/A).

The Panel

  1. Create a panel (or enable the node/%node default panel and add a variant).

  2. In context tab, Create a relation to taxonomy term on the entity.

  3. Click add content in one of panes.

  4. If you didn't change the view's category, it's in views pane add it. It will ask for an Entity ID and Term ID both are available now in your panels context, set them.

  5. Now when the panel is displayed, All the content with with the same term excluding the content on this page are displayed.

Views Configuration

Views Configuration

Contextual filter

Contextual filter

Argument input

Argument input

Important P.S:

This method is tried and it works flawlessly, but at the time of writing this article ctools had a bug. There is a patch for it: https://www.drupal.org/node/2195211

hkoosha
  • 1,136
  • 2
  • 15
  • 30
-2

I am not sure if I fully got the gist to your question but I think node queue is the answer. Check out http://drupal.org/project/nodequeue

The module is well developed. I have used it for a client and you can use their smartqueue api too make make your list.

Hope I was helpful.

Vishal Khialani
  • 2,557
  • 6
  • 38
  • 49