8

I've created a view with a set filters exposed (to show filtering options in the page). And one of those filters is a select field with all the taxonomy terms of a vocabulary.

That's ok, it's showing now. But I want to only show the taxonomy terms that have at least one node associated to it. To avoid empty results if selects an empty term.

Anyone knows how to do that?

Thanks.

Leandro Ardissone
  • 3,009
  • 6
  • 32
  • 31

4 Answers4

9

Solution in views 3 (which may work in views 2)

Set a relationship for the view to join the terms to the nodes (in views 3 this is 'Taxonomy term: Content with term')

Then in the options tick 'Require this relationship' (this excludes terms without any nodes)

This may produce duplicates, in views 3 you open up Query settings, and tick Distinct (this will omit duplicates)

sphism
  • 119
  • 1
  • 3
  • This works, but didn't get what's wrong with duplication, didn't notice any troubles. – Rantiev Apr 30 '14 at 13:58
  • Excellent answer! I indeed had to use Distinct to prevent the term from being output for each node that references to it. Thanks a lot! – lmeurs Dec 29 '14 at 18:27
1

This is a simpler way. Hope it helps.

Add a relationship, filter for terms and choose "Taxonomy term: Representative node". Check the "require this relationship" and thats it. Optionally tick "Query settings > Distinct" in case of duplicate terms.

Based on http://www.waldbeek.com/blog/view-taxonomy-term-and-hide-empty-terms

lmeurs
  • 16,111
  • 4
  • 27
  • 30
Jessica K
  • 137
  • 7
  • While that link may contain an answer, it is preferable to include a brief summary, rather than just a link. That way the answer remains useful to others even if the link breaks or changes. – Leigh Nov 03 '13 at 14:53
  • Great method since it supports more settings than user1191001's answer. Thank you! – lmeurs Dec 29 '14 at 18:44
1

This would be a great feature for Views to have and you should submit a feature request to the maintainers for it. Unfortunately you can't do this with the existing Views code.

\What you could do, though, is see how Views constructs a filter control in its own source code, and implement that function yourself and add in an extra check to see if there are any posts with that term or not, and not display that term if there aren't any.

alxp
  • 6,153
  • 1
  • 22
  • 19
-1

With views 3 this gets very simple using aggregation! I'm posting this replay because I spent a lot of time discovering it. Everything is very well explained in this screencast: http://dev.nodeone.se/en/the-aggregation-setting-the-emmajane-episode

Matteo
  • 1
  • Hello and welcome to StackOverflow. Are you sure that you can display TAXONOMY TERMS with at least one node tagged ? Aggregation, if used in a taxonomy view will not filter terms with no terms IMO. But in a node-view, it will probably – AKS Oct 29 '12 at 13:08