I face a rather common problem of showing related nodes to the current node in a block using taxonomy terms. I am using latest drupal 7 and views module

I've found a couple of suggestions 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 http://www.webomelette.com/related-content-views-taxonomy-drupal. Despite that I follow these to the letter and make exact duplicate views, no nodes get displayed.

Another method provide a year back, just doesn't seem to work with the latest Views 3.7.

  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

I'd appreciate any feedback that can lead to a working solution.

Comments

WorldFallz’s picture

can't speak to the other 2 methods, but i use the one described in the webomelette post and it works fine. maybe give it another shot?

crz_06’s picture

I've tried several times ;) Could it be that it doesn't work for me because my URL doesn't include the taxonomy terms. As well not the node ID, because I use an alias instead?

What I tried, no success though is to replace CONTEXTUAL FILTERS Content: Has taxonomy term ID/ axonomy Term ID from URL and replace this with Taxonomy Term ID from URL

  1. Add a Relationship on your View: "Content: Taxonomy terms on node"
  2. Add a Contextual Filter: "Taxonomy Term: Term ID"

configure the Contextual Filter:

  1. Check the "Provide default value" radio button
  2. On the Type drop-down, select Taxonomy term ID from URL (normally, this wouldn't work since we aren't passing the value through the URL, but following is the work-around)
  3. Check "Load default filter from node page"
  4. Check "Limit terms by vocabulary"
  5. Check the vocabularies you want to link to from the node (there should be a list of your vocabularies with checkboxes next to them).

PS: my articles have more than one taxonomy term associated with them

Second question (referring to my last method posted above): In the Configure Relationship: Content: Taxonomy terms on node, under Vocabularies, there is an Identifier field with the default text value "term". Not sure what is the purpose of this one, should I replace this with my taxonomy terms? or leave blank?

WorldFallz’s picture

The only thing that matters to the webomelette method is that the view appears on a node page-- doesn't matter if the node has an alias or that there are multiple terms. I just did it again, and it definitely should work.

As for your other question, if i understand the question, the identifier is just so you know which filter that is in the views ui-- it has nothing to do with the actual view functionality.

crz_06’s picture

Ok, strange .. I have page--node-- template, and replictaed the webomelette solution, no effect .. arghh.. Maybe i'll just leave it now, been causing me days of frustration

WorldFallz’s picture

hold up... you didn't say anything about templates. what does page--node.tpl.php have to do with anything here? how are you trying to use the view?

tterranigma’s picture

You need to add another contextual filter first, eg nid, that will catch the nid from the url (eg node/432) so that the Taxonomy Term contextual filter (the second in line) will have nothing to catch and the "Provide default value" part will kick in.

vchen’s picture

This helped me a lot! I've been trying to figure contextual views with block displays for months until I found this tutorial and it got it working like that! All you need for this is views and taxonomy. Tip: the pieces of content that you're trying to relate have to use the same taxonomy field.

http://www.pixelwrapped.com/blog/related-articles-block-view-nodes-commo...

Hope this helps!

millionleaves’s picture

Thanks for this tip. I normally link node types directly together using entity reference fields (one node type uses an Entity Reference field to relate that node type to another node type) rather than via common taxonomy term. Creating contextual views in that scenario works fine.

I found this page after using a Term Reference field on one node type and an Entity Reference field for the taxonomy term on the other, and my view didn't give any results. I didn't know it was the fact that I'd used different fields for the taxonomy term. Replacing the Entity Reference field with a Term Reference field did the trick for me. Thanks - I'm not sure I'd ever have thought of that.

I provide Drupal, Drupal Commerce and CiviCRM development services for customers in New Zealand and beyond

Jason Wells’s picture

I'm still facing this problem using Views 7.x-3.7 with Drupal 7.27. I've tried every possible suggested solution and can't get this to happen.

Basically, I have a collection of PDF's using a 'Download' content type, using taxonomy 'pre-boarding-academy' to assign it a category, and I want the view to display all the PDF's in the category when the user is on the page

http://anyurl.com/summer-programmes/pre-boarding-academy

Any other suggestions?

marcoka’s picture

i have exactly the same problem. and i 100% sure taht it worked before.

thomasmurphy’s picture

How I generally do this is by using two relationships and a one contextual filter which uses the second relationship.

1st relationship:
Content: Taxonomy terms on node
pick the required vocabulary [vocabulary] (required)

2nd relationship: (only available after setting up the first one)
Taxonomy term: Content using [vocabulary] (required)

Contextual filter:
This is slightly counter-intuitive, because you are filtering based upon the page you are on, so you need to filter by
Content: Nid > used the 2nd relationship, this is the relationship between the node you are on and the terms it has.
Just leave the default "Display all results for the specified field" and you should have a working related content block.

Don't use "Content: Has taxonomy term ID" this is for path based arguments, also useful, but different.

Proteo’s picture

It worked great.

mocasalter’s picture

Thanks so much! This worked for me with one small modification.

I had to change the settings in Contextual filter > Content: Nid > When the Filter Value is Not Available. Instead of the default "Display all results for the specified field," I had to use " Provide default value: Content ID from URL."

I have no idea why that fixed it, I wasn't seeing any changes in my block otherwise, but I don't even care anymore lol I'm just glad it works!

dzinelabs’s picture

Well, this works sort of ...I use Drupal 8 and it seems that some filters have changed in views:

- 2nd relationship: (only available after setting up the first one)
Taxonomy term: Content using [vocabulary] (required): In D8 it seems that does not exist anymore so the one I came up with is Taxonomy term: Content using field_tags with th only option to use term

- contextual filter: Content Id with relationship field_tags

This results in related content but has several flaws:
1) duplicated content
2) not excluding the current node the user is viewing

I tried to resolve the duplicate content by setting the Query settings to distinct but this did not help

As far as not excluding the current node the user is viewing, in the contextual filter: Content Id with relationship field_tags I checked exclude box but also no go

To sum it up:

this might work in D7 but in D8 seems to be incorrect.

I am googling for days now how to have a related content block in D8 without duplicates and excluding the current node but all I get is stuff on D7 and as I mentioned it seems the filters in D8 changed ...Has anybody here managed to get such a basic thing working in D8?

thomasmurphy’s picture

Excluding the current node, did you try
https://www.drupal.org/node/131547

Don't use a relationship for this one as it's just a reaction to the current page.

reducing duplicates can often be complex, using distinct and pure distinct under query settings can help.

dzinelabs’s picture

yup, gone over the that post but I can not get it to work.

Also, query settings only has distinct and that also does not work.

Now this is in D8 ... Is D8 unable to achieve this or am I getting so stupid ... I used to achieve easily in D6 ...

thomasmurphy’s picture

Hmm, fair enough, I haven't built one in D8 yet, but once I have I'll post back :)

dzinelabs’s picture

Well seems I have figured it out in D8 ...Don´t know what I did different from all the tuts ... For those interested, here´s how I achieved it:

1)No relationships
2) add 2 contextual filters:
- content: has taxonomy term ID
- Content: Id

Configuration:

1) content: has taxonomy term ID filter:
provide default value, type: taxonomy term ID from Url
Load default filter from node page, that's good for related taxonomy blocks
Limit terms by vocabulary: choose vocabulary
Filter to items that share all terms
Uncheck everything else

Uncheck everything in:
Exceptions, When the filter value IS available or a default is provided, More

2) Content: Id filter:

Provide default value: content ID from URL
Uncheck everything else

Uncheck everything in:
Exceptions

Check in More:
Exclude
Uncheck everything else

azafor’s picture

There's a module called "Views Related Content", that is what you need to meet your expectation. I've used this on my site. All you have to do is,

  • Install Views Related content
  • Create a Views block of content type
  • Add a content filter as "Related Content: Term"
  • Select the vocabulary you wish to short by

That's it, Place the block from Block administration page.

I've an in-depth tutorial about this here: https://pointblankpro.com/related-content-block-drupal this should help you in configuring related content block.

sprite’s picture

Following thread to record helpful note above.

spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...

dzinelabs’s picture

The tutorial works like a charm.

Tnx.

monorailmedic’s picture

I set this up earlier today (using the Views Related Content) and it works great, but it suggests the node you're already looking at some times (depending on the publish date, as best as I can tell.

Am I missing a way to solve for this?

dzinelabs’s picture

@mono railmedic: instead of the VRC module, set it up with views:

E.g. create a view that will show related content based on articles.

The way the view works is that it is based on content type and taxonomy terms. In this example the content type is article but you must have set up a taxonomy with terms, since you will need to tell views to sort on taxonomy terms that are linked to your articles.

1) create a new view and name it e.g. Related Content. Choose content of type: "article". Check "create a block" and leave the rest as default. Hit save and you will be directed to the view display where you can adjust the view.

2) on the view display page choose "unformatted list". This will show the related content in a list without bullets (unless you want bullets in which case you can choose another format).

Under the "Fields" section you will have the field called "Content: Title". That´s by default since you opted for content when you created the block. This field will show only the title of the related article once the view is created. If you like to add other fields, just go ahead.

For the time being, you can leave the other settings of the view at their default settings. The only thing you might change is the pager setting: set it to display a specified number of items, e.g. 5.

3) Now add contextual filters to sort content based on the taxonomy terms.

Just follow these steps:

-Add a contextual filter "Has taxonomy term ID"
-Select "Provide default value"
-Select "Taxonomy term ID from URL" as type
-Uncheck "Load default filter from term page"
-Check "Load default filter from node page, that's good for related taxonomy blocks"
-Check "Limit terms by vocabulary"
-Select the vocabularies that you want to short by
-Select "Filter to items that share any term"
-Scroll down and check "Reduce duplicates", this will prevent the same content from being displayed multiple times
-Click on the Apply button to save the contextual filter and save the view

Your Related Content block is ready.

Place your block and you will see that the block will be populated with all the content (in this case 5 since we have set the pager to 5 items) that has the same taxonomy terms. But you´ll notice that the node you are viewing is also listed. (your problem). Here´s how to exclude the current node:

-Add another contextual filter "Content: ID". If you use D7 the filter is called "Content: NID"
-Select "Provide default value"
-Select "Content ID from URL" as type
-Scroll down the page to the "More" drop down
-Check "Exclude" and click apply

Save the view and you´re done.

JohnnyW’s picture

You made that too easy :)

joro78’s picture

I have an issue concerning the related content by taxonomy term.
I am using views and contextual filters such as Nid and Has taxonomy term ID. However the related content is also from the other categories, under the root term. Is it possible to limit the view, only to the same level of taxonomy?

For instance showing related content to viewed node (based on terms in node), corresponding to category 1, 2 or 3.

Category root term
-category 1
--child
-- child
-category 2
-- child
-- child
-category 3
-- child
-- child

Any help would be appreciated.

dzinelabs’s picture

does this set you in the right direction?

https://www.ostraining.com/blog/drupal/specific-terms/

joro78’s picture

Thank you for referencing that, dzinelabs.

However I have found a solution based on this discussion here:
https://www.drupal.org/node/1188906#comment-5246254

This was the most useful one, in my case.

And it works just fine =)

dzinelabs’s picture

great. which method did you use (for future reference)

joro78’s picture

I am using two Contextual filters:
(my related content is view of nodes, sharing taxonomy terms)

first filter: Nid providing default value Content ID from URL;
Exclude is marked here in the more section;

second one: Content Has taxonomy term ID;
When the Filter value is not available > Provide default value > Taxonomy term ID from URL;
Load default filter from node page, that's good for related taxonomy blocks (checked);

Multiple-value handling > Filter to items that share all terms (checked);
in more section here > Allow multiple values (checked);
I think that these are the crucial options here.

I did not check Limit terms by vocabulary in this filter,
because I wanted to use it with all the ones I have in site.

Works like charm.

dzinelabs’s picture

nice. when reading your post my first idea was going with taxonomy term id with dept but trying your set up did not produce the result you were looking for. Anyway, glad it worked out for you.

Gaurav.Singh’s picture

Hi,
Just adding one contextual filter "Content: Has taxonomy term ID" worked for me.
Configure it to:
When the filter value is NOT available -> Provide default value -> Taxonomy term ID from URL

Check Preview with contextual filters:
use your term id and test it

Once tested can merge block to any node page and check results.

MahmoodZidan’s picture

Hello there, I didn't follow anything in the comments, I just followed what you typed in the main post and it worked like charm. Thank you.