Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display only taxonomy terms with nodes associated to in Views 2?

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.

like image 736
Leandro Ardissone Avatar asked Jan 07 '09 20:01

Leandro Ardissone


3 Answers

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)

like image 85
sphism Avatar answered Oct 31 '22 11:10

sphism


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.

like image 42
alxp Avatar answered Oct 31 '22 09:10

alxp


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

like image 36
Jessica K Avatar answered Oct 31 '22 11:10

Jessica K