3

Quick view: I want to create a views block that lists all other nodes that have the same taxonomy term(s) and I want to group the related nodes by term.


I have trouble while doing this:

The content type has some taxonomy terms fields and one of them is my_taxonomy_field. I can enter multiple values to this field.

What I basically want is to show all other nodes tagged with the term(s) on this node and group them by terms. I could do this by contextual filters (content: has taxonomy term ID + ) as explained here or here.


Let me explain:


This is my node list:

  • NODE 1 taxonomy term field: term 1, term 2, term 3

  • NODE 2 taxonomy term field: term 3, term 4

  • NODE 3 taxonomy term field: term 5, term 6, term 7

  • NODE 4 taxonomy term field: term 6, term 7

  • NODE 5 taxonomy term field: term 1, term 3

  • NODE 6 taxonomy term field: term 1

  • NODE 7 taxonomy term field: term 8

  • NODE 8 taxonomy term field: term 2, term 9


Let's look at NODE 1, it has these terms: term 1, term 2, term 3

And the terms are tagged on that nodes:

term 1: node 1, node 5, node 6

term 2: node 1, node 8

term 3: node 1, node 2, node 5


This is the related nodes block based on the taxonomy term field:

  • node 1 (excluded)
  • node 2
  • node 5
  • node 6
  • node 8


I want to group them by terms as that (note that current node (node 1) excluded):

BLOCK

  • TERM 1
    • node 5
    • node 6
  • TERM 2
    • node 8
  • TERM 3
    • node 2
    • node 5


I tried to this with that way: views -> style -> settings (grouping field) and I selected the taxonomy term field. It does what I want, not what I want actually, more than what I want because it creates a views block as that:

BLOCK

  • TERM 1
    • node 5
    • node 6
  • TERM 2
    • node 8
  • TERM 3
    • node 2
    • node 5
  • TERM 4
    • node 2
  • TERM 9
    • node 8


This views doesn't add any (irrelevant) nodes to the list, the nodes are the same with the un-grouped one but because node 2 and node 8 are tagged with other terms, other terms listed here.


How I could list related nodes as grouped but without this 'unwanted' terms, with only the terms in the current node?

(Note: I'm working on both two ways mentioned above, these are the screenshots of them):


Method 1:

Way 1


Method 2

Way 2

2
  • I tried 5-6 methods to solve the problem but couldn't figure out it. Any idea?
    – herci
    Commented Dec 3, 2014 at 21:24
  • 1
    J.Reynolds solution below looks like the right way to go. Ignore mine! Commented Dec 14, 2014 at 7:48

3 Answers 3

6
+50

I've used tags as the term field, so adjust for that.

Create the block view as follows:

A view of content (nodes).

Relationships:

  1. Content: Tags (field_tags), require relationship // you will use your term field here

  2. Taxonomy term: Content using Tags (Relate each Content with a Tags set to the term.) and set the relationship to the first relationship added, and tick require this relationship // yours will be named differently

Contextual filters:

  1. Content: Nid (node id). Set the relationship to the second relationship added. Provide a default value of Content ID from URL for WHEN THE FILTER VALUE IS NOT AVAILABLE. Specify a validation criteria of Content -> your content type -> Node ID -> Hide view

  2. Content: Nid (node ID). DO NOT USE A RELATIONSHIP. Provide a default value of Content ID from URL for WHEN THE FILTER VALUE IS NOT AVAILABLE. Specify a validation criteria of Content -> your content type -> Node ID -> Hide view. Click on more and tick Exclude.

Fields:

  1. Content Title (NO relationship)

  2. Taxonomy term: Name, use relationship 1, and hide from display

Sort Criteria

  1. Taxonomy term: Name, use relationship 1, ascending

  2. Content Title, ascending

Filter Criteria

Add a filter of Content (node) type to filter on your content type.

Format:

Unformatted list, with settings: Grouping field Nr.1 set to the term field (field 2)

6
  • 1
    I have not ever used a relationship on a relationship within views, useful information, thanks Commented Dec 14, 2014 at 7:46
  • Thanks, thanks a lot. It seems working correctly and I never thought a method as yours. Only one thing to say: Contextual filter first step; it should use the second relationship not the first one because the first one not selectable. I will check this for many nodes and say the results (for now, it's working well). Thanks again.
    – herci
    Commented Dec 14, 2014 at 21:03
  • @herci Yes, thanks. Glad it worked. I see I did mention the wrong relationship and corrected the answer. Commented Dec 15, 2014 at 3:20
  • @herci - could you mark this the correct answer to give J.Reynolds credit? Commented Dec 15, 2014 at 9:58
  • @autopoietic yes I did after checking possible situations. And also thanks for your effort.
    – herci
    Commented Dec 15, 2014 at 19:37
1

One possible solution would be to pass in taxonomy term ids from node 1, before you even start building the view.

This might involve creating a custom block (preferably by making very simple block module). The block would simply node_load(1), get the taxonomy terms from the node object.

The block could now embed the view in the block content using views_embed_view, passing the term ids into the view as multiple arguments (eg 7+6+46), and possibly the node id (to exclude). Now the view only needs to list nodes associated with these term ids, excluding the nid, and grouping by term. You could try excluding the nid using 'id from url', but I am not sure whether this works with views_embed_views, and if you do, watch out for block caching.

Given that node and taxonomy tables should be relatively stable (being in core) you might actually find this relatively easy to do with a simple db query in your block, though then you lose the extended configurability of views.

There may well be a way of doing this all within views, but I can't think of it presently, as the logic suggests a subquery (views relationships?).

1
  • Thanks, I'm a bit far away the method you mentioned about but I will work on it; it seems good to me.
    – herci
    Commented Dec 10, 2014 at 21:27
0

As per this blog, you need to follow steps to display related content.

  1. Create view (block)
  2. Add fields (title, a small image, date, whatever you like). Or teasers, what works as well.
  3. “Arguments” is from now on called “Contextual filters”, so go there.
  4. Add the filters: “Content: Has taxonomy term ID (with depth)” and “Content: Has taxonomy term ID depth modifier”
  5. Configuration on this filter: → When the filter value is NOT in the URL: → Provide default value → Type: Taxonomy Term ID from URL → under that, check the box “Load default filter from node page, that’s good for related taxonomy blocks.” → now check the boxes of the taxonomies you want involved here
  6. Now you need to add the second filter in order to exclude the current node from the list. Click [+] button of the contextual filters section.
  7. Select Content: Nid
  8. Set the ‘when argument is not present’ to ‘Provide default argument’
  9. Set the ‘Default argument type’ to ‘Content ID from URL’
  10. Go to “MORE” , and check the “Exclude” box.
  11. Move the filter “Content: Nid” to the top position.
  12. Add a block to your view and embed the block on your node pages
  13. Save your view

Grouping :

After following the steps you need to Grouping your Fields using Taxonomy Term. You can group the items in view's Style Settings.

enter image description here

4
  • The problem is not about displaying the related content. I have already mentioned about the method of the blog you said as Method 1 in my question. The problem is about grouping. Please, read the question (again).
    – herci
    Commented Dec 9, 2014 at 12:21
  • I added how to grouping in the last of my answer. There is an option to group it based on field in Style settings. Commented Dec 9, 2014 at 12:24
  • Check updated answer. Commented Dec 9, 2014 at 12:26
  • Have you read the question? I do know how to group the results and I have already said that in my question. While grouping the results, some other taxonomy terms appear because these terms are tagged together with the terms on this node in another nodes. Please, re-read the question or just read the whole question.
    – herci
    Commented Dec 9, 2014 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.