<p><strong>First</strong>: grab all entries that match your criteria</p>

<p><strong>Second</strong>: grab all Tags related to your entries</p>

<h3>PHP</h3>

<pre><code>$entryIds = Entry::find()
    -&gt;yourTagField(':notempty:')
    -&gt;fellowStatus('1')
    -&gt;section('fellows')
    -&gt;ids();
$tagsRelatedToEntries = Tag::find()
    -&gt;relatedTo($entryIds)
    -&gt;all();
</code></pre>

<h3>Twig</h3>

<pre><code>{% set entryIds = craft.entries
    .section('fellows')
    .fellowStatus('1')
    .yourTagField(':notempty:')
    .ids() 
%}
{% set tags = craft.tags.relatedTo(entryIds).all() %}
</code></pre>

CC BY-SA 3.0
2018-04-30T18:33:48.380Z
2018-04-30T18:33:48.380Z
Robin Schambach
<p>I want my <code>&lt;ul&gt;</code> to show a <em>single</em> tag representing all "active" entries ( "active" using a custom lightswitch field in the entry: <code>fellowStatus</code>.</p>

<p>Each tag is a year (e.g. 2014, 2010, etc)</p>

<p>E.g. if there are no "active" entries with the tag <code>2017</code>, it should not display.</p>

<p>The code block below spits out only tags where entries are "active" 
BUT...
It spits out duplicates of each tag depending on how many entries are using it. (e.g. It displays 8 instances of <code>2015</code> instead of 1)</p>

<pre><code>&lt;ul&gt;
{% for entry in craft.entries.section('fellows').limit(null) %}

  {% set tags = craft.tags.relatedTo(entry).limit(1).find() %}
  {% set active = entry.fellowStatus %}

    {% if entry.fellowStatus == 0 %}

      {% for tag in tags %}
          &lt;li&gt;
            &lt;a href="{{ tag }}"&gt;{{ tag.title }}&lt;/a&gt;
          &lt;/li&gt;
      {% endfor %}

    {% endif %}

{% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2018-04-27T04:36:21.770Z
Do you want to show all tags related to your entries or do you want to display all tags for each entry?
Robin Schambach
CC BY-SA 3.0
2018-04-30T18:15:29.173Z
I want to show all tags that are in use. 

E.g. 
If `2015` has an entry, show.
If `2001` doesn't have an entry, don't show. 
If `2017` has an entry, but `fellowStatus` light switch is off, don't show.
Andrew Hahn
<tags><forloop>
My `for` loop has fallen...and it can't get up
# My `for` loop has fallen...and it can't get up

(Asked by: username_0 on 2018-04-27)

I want my  to show a single tag representing all "active" entries ( "active" using a custom lightswitch field in the entry: fellowStatus.
Each tag is a year (e.g. 2014, 2010, etc)
E.g. if there are no "active" entries with the tag 2017, it should not display.
The code block below spits out only tags where entries are "active" 
BUT...
It spits out duplicates of each tag depending on how many entries are using it. (e.g. It displays 8 instances of 2015 instead of 1)

{% for entry in craft.entries.section('fellows').limit(null) %}

  {% set tags = craft.tags.relatedTo(entry).limit(1).find() %}
  {% set active = entry.fellowStatus %}

    {% if entry.fellowStatus == 0 %}

      {% for tag in tags %}
          
            {{ tag.title }}
          
      {% endfor %}

    {% endif %}

{% endfor %}

**Tags:** tags, forloop
> **Comments:**
> **username_1**: Do you want to show all tags related to your entries or do you want to display all tags for each entry?  
> **username_0**: I want to show all tags that are in use.   E.g.  If `2015` has an entry, show. If `2001` doesn't have an entry, don't show.  If `2017` has an entry, but `fellowStatus` light switch is off, don't show.  

---

## 1 answers

### Answer by username_1 on 2018-04-30. Score: 1

First: grab all entries that match your criteria
Second: grab all Tags related to your entries
PHP
$entryIds = Entry::find()
    ->yourTagField(':notempty:')
    ->fellowStatus('1')
    ->section('fellows')
    ->ids();
$tagsRelatedToEntries = Tag::find()
    ->relatedTo($entryIds)
    ->all();

Twig
{% set entryIds = craft.entries
    .section('fellows')
    .fellowStatus('1')
    .yourTagField(':notempty:')
    .ids() 
%}
{% set tags = craft.tags.relatedTo(entryIds).all() %}


---

