View by Taxonomy Term - hide empty term
I've searched through through all the posts that may be slightly relevant and tried everything that looks right in constructing the view, but cannot get this to work.
I've constructed a View using the Term view type that pulls out all the terms in a certain vocabulary and displays them in a nice grid (with an attached image in a CCK field for the term).
But I need to be able to hide terms that have no content in the results (or only unpublished content) and whether I choose Hide Empty Fields in Style Settings > Row Style: Fields or in Fields > Taxonomy Term, the output still displays the field even though there are no nodes tagged with the term.
Help would be much appreciated.
Comments
Not sure this is the
Not sure this is the solution.
Add field Views Result Count (in Global set) to the view, and set Hide If Empty.
What's new and changing in PHP 8.4
So close
Not quite what was needed, but pointed me in the right direction. I needed to add a Filter for File Usage: Use Count and set it to greater than 0 - worked a treat.
Many, many thanks.
Spoke too soon
On further testing I've realised this didn't work - it has simply removed terms that don't have an image attached from the output.
:( let me try myself and find
:(
let me try myself and find a hint for you.
What's new and changing in PHP 8.4
If you find one, like to know
If you find one, like to know a resolution to. Maybe it is not possible at this moment with views and it will be a feature request?
Solution
There's a lazy way to do it with Relationships, just add the "Taxonomy: Node (Get all nodes tagged with a term.)" relationship and check the require this relationship-box. Any ter without nodes will be excluded.
Not sure if this is optimal from a performance point of view. Anyone care to enlighten us?
Drupalutvecklare | Drupal Developer in Sweden
it works
I tried your solution, and it works.
But now there are duplicates terms in the view.
I try to check "Distinct" options in the "settings of query" but it doesn't work.
resolved: i enable
resolved:
i enable "Aggregation Mode", and set it to "Minimum" on the field "Taxonomy Term"
(the "Distinct" option in the "Query Setting" must be enabled anyway)
In relationships I used
In relationships I used "Taxonomy term: Content with term" and checked the Required Relationship. That seemed to work for me.
none of the sugested solutions don't work for me
"In relationships I used "Taxonomy term: Content with term" and checked the Required Relationship. That seemed to work for me."
If a use relationships i get then all taxonomy terms...
I can confirm it works...
To recap, what happens is that when you show the taxonomy terms in a view, you get all the terms even if they are empty. Here's how it worked for me.
1. Relationships > "Taxonomy term: Content with term" and checked the Required Relationship.
2. Query settings > Distinct.
does not work for me!
I tried the above mentioned solution. It makes sure that terms that are not used in any content do not appear in the view. But it still shows terms that are connected to unpublished nodes.
I had to figure out another solution. I used Views PHP module and wrote a php filter in view's filter criteria. I wrote the following code in the php filter:
I had this variable $row->tid because I had a hidden tid field.
You rock
Thanks so much for posting this. I was having the same issue and your code works great. Never knew about the php views module.. now we can really have some fun!
cheers
-z
A non coding method would be
A non coding method would be add a taxonomy filter to NOT show the term with unpublished content.
this seems to work except
this seems to work except when a term has children. For example, if I have the following:
A
B
--1
--2
C
and I have nodes tagged with A, 1, and 2 but not B or C, then B and C will not show up in the view. However, I would like for B to show up since its children are attached to nodes. Any ideas how I can get this to work?
Thanks!
I think this is
I think this is simpler.
http://www.waldbeek.com/comment/reply/494
that requires a more complex solution
This cannot be done by requiring a relationship. If you have a hierarchy, and you want to exclude terms that have no content UNLESS one or more of that term's child terms do have content, you really need to use Views PHP. Use a "Global: PHP" field as a filter, and do this:
Before coming to that solution, I did try something similar using a computed field, but with no luck. The field would compute correctly, but for some reason my view had a problem with using a computed field as a filter.
If you need to go deeper than one level, this becomes much more tricky. But, I've had good results by using EVA to add views as fields on the taxonomy term, and adding a custom display mode with Display Suite. This way when you create a view that pulls taxonomy terms, you have the view render the terms with the custom display mode. I'll admit, that's a pretty heavy solution. Display Suite, EVA, and Views PHP are no joke. But, if you do it correctly, you can simulate recursion through the term hierarchy.
I'm sorry it took 4 years to get an answer. Hopefully you figured it out a long time ago.
I use views PHP field and put
I use views PHP field, on Drupal 6 site, and put that code to "output code" box:
So, I exclude not puplished items from count.
need count query
how can i write following sql query not with view in D6
vocabulary :
term 01
term02
node :
testnode01 : with term 01
testnode02 : with term 02
testnode03 : with term 01
now I need a result
testnode01 , testnode03 : term count (1)