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

ayesh’s picture

Not sure this is the solution.
Add field Views Result Count (in Global set) to the view, and set Hide If Empty.

Matt113’s picture

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.

Matt113’s picture

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.

ayesh’s picture

:(

let me try myself and find a hint for you.

Jurjen de Vries’s picture

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?

Andreas Radloff’s picture

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

brankolo’s picture

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.

brankolo’s picture

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)

gratefulsk’s picture

In relationships I used "Taxonomy term: Content with term" and checked the Required Relationship. That seemed to work for me.

hrpo’s picture

"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...

kabarca’s picture

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.

quickly’s picture

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:

$count = db_query("SELECT COUNT(*) FROM {taxonomy_index} where tid = $row->tid")->fetchField();
if ($count == 0){
return TRUE;
}
else{
return FALSE;
}

I had this variable $row->tid because I had a hidden tid field.

ascii122’s picture

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

jessicakoh’s picture

A non coding method would be add a taxonomy filter to NOT show the term with unpublished content.

zdean’s picture

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!

jessicakoh’s picture

eriktaylor’s picture

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:

$count = db_query(
             'SELECT COUNT(nid) FROM {taxonomy_index} WHERE tid IN (' . 
             'SELECT tid FROM {taxonomy_term_hierarchy} WHERE ' .
             'tid = :tid OR parent = :tid)',
             array(':tid' => $data->tid)
         )->fetchField();

if ($count == 0) {
    return true; // No content associated with this term or child term(s).
} else {
    return false; // This term or a child has associated content.
}

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.

velpan’s picture

I use views PHP field, on Drupal 6 site, and put that code to "output code" box:

<?php     
   $sql = "SELECT COUNT(tn.nid) 
               FROM {term_node} as tn
               INNER JOIN {node} as n USING (nid)
               WHERE tn.tid = %d AND n.status = 1";

   echo db_result(db_query($sql, $row->tid)); 
?>

So, I exclude not puplished items from count.

surendra77c’s picture

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)