15

I have have a vocabulary structured as so:

  • Root Alpha
    • Term Alpha 1
    • Term Alpha 2
      • Subterm Alpha 1
  • Root Beta
  • Root Gamma
    • Term Gamma 1
    • Term Gamma 2

How do I filter the view to show only the root terms of the vocabulary? Currently my view is displaying the root terms as well as the children (Term * *)

Going forward from that I would like to pass the argument 'Root Alpha' (or its tid, or whatever from a context) and then have it filter to only 'Root Alpha's direct children. (Term Alpha *). Filtering to a depth any further than that would be a bonus.

I can get it to work if I put all my terms in one root term so that the vocab becomes:

  • True Root
    • Root Alpha
      • Term Alpha 1
      • Term Alpha 2
        • Subterm Alpha 1
    • Root Beta
    • Root Gamma
      • Term Gamma 1
      • Term Gamma 2

I'm not sure which vocab structure is more correct, writing this made me realise I have some reading to do apart from figuring this out with views.

13

For displaying only the root terms, I have a solution which surely will make someone facepalm (at least I did that):

  1. Add a contextual filter to your taxonomy view
  2. Select Taxonomy term: parent term.

    Set it up like this: When a filter value is not available: Provide default value - Fixed value. Set the value to 0.

Yeah, that's all. Terms without parent have 0 here. See the description of the parent field of the taxonomy_term_hierarchy table:

Primary Key: The {taxonomy_term_data}.tid of the term's parent. 0 indicates no parent.

  • Thank you very much, definitely facepalmed myself for this one. Just as a note if someone were to get stuck with this, you can't specify 'Taxonomy term' as the validator when doing this, as there is no term with tid 0. – Phizes Feb 20 '12 at 19:05
2

if you have a static taxonomy structure, you can filter using filter criterion: Content: Has taxonomy terms (with depth) then in this place you can select just the root terms to filter:

filter criterion: Content: Has taxonomy terms (with depth)

I hope information be useful.

  • Taxonomy structure won't be static, and this wouldn't allow me to pass an argument and then filter to the children of a root term. – Phizes Oct 27 '11 at 15:30
  • @moon.watcher:How can I use taxonomy term(with depth)?I am selecting only the parents but the children also appear. – sokratis Mar 10 '12 at 13:22
1

You can also do it with a simple filter, and a non-static taxonomy structure :

  1. Add a filter criterion : Taxonomy term : parent id
  2. Select operator "Is equal to" and fill the value with "NULL"

enter image description here

It will display only the root terms of your taxonomy, and will still work if you change the taxonomy structure later...

  • Yes, @scandel it would handle a non-static taxonomy structure, but it would not handle going further down the tree as laid out in the question, the accepted answer does. – Phizes Sep 28 '14 at 7:32
0

You can select it with WHERE parent = 0 using db_query() in your page.

  • I'm not sure what you mean by "in your page", please explain? Also I'm not executing the query myself, views is, so wouldn't I have to rather alter the query? – Phizes Dec 13 '11 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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