<p>What you are looking for is the <code>relatedTo</code> parameter (<a href="https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#relatedto" rel="nofollow noreferrer">docs</a>) and <a href="https://craftcms.stackexchange.com/questions/32950/display-entries-by-tag/32963#32963">this post</a> will also help.</p>

<p>If you wanted to return any entries which has (any) tags attached, you could do:</p>

<pre><code>$entries = Entry::find()-&gt;tags(':notempty:')-&gt;all();
</code></pre>

<p>Where <code>tags</code> is the handle of your tags field.</p>

CC BY-SA 4.0
2019-11-12T11:42:50.327Z
I've used the `relatedTo` parameter when trying to find a post with a specific tag - for example `Entry::find()->relatedTo($tag)->all();` where `$tag` is a particular tag - how would I abstract this to find a post with any tag though?
Alk
CC BY-SA 4.0
2019-11-12T14:45:56.517Z
Just edited my answer above.
Oli
CC BY-SA 4.0
2019-11-12T11:41:12.767Z
2019-11-12T14:45:40.633Z
2019-11-12T14:45:40.633Z
Oli
<p>I'm trying to get all the entries which have tags associated with them. </p>

<p>At the moment I'm doing: </p>

<pre><code>$entries = Entry::find()-&gt;with('tags')-&gt;all();
</code></pre>

<p>and then filtering the results in PHP as follows: </p>

<pre><code>$filteredEntries = array_filter($entries, function($entry) {
    return count($entry['tags']) &gt; 0;
});
</code></pre>

<p>This works fine, but I am still fetching all the entries, even without tags from the DB - I think this can be more efficient if I can specify in my query that I only want the entries with tags associated to them. How can I do this?</p>

<craft3><entry><php><tags><yii>
Fetch Entries with Tags Only
# Fetch Entries with Tags Only

(Asked by: username_0 on 2019-11-12)

I'm trying to get all the entries which have tags associated with them. 
At the moment I'm doing: 
$entries = Entry::find()->with('tags')->all();

and then filtering the results in PHP as follows: 
$filteredEntries = array_filter($entries, function($entry) {
    return count($entry['tags']) > 0;
});

This works fine, but I am still fetching all the entries, even without tags from the DB - I think this can be more efficient if I can specify in my query that I only want the entries with tags associated to them. How can I do this?

**Tags:** craft3, entry, php, tags, yii


---

## 1 answers

### Answer by username_1 on 2019-11-12. Score: 1 | Accepted answer

What you are looking for is the relatedTo parameter (docs) and this post will also help.
If you wanted to return any entries which has (any) tags attached, you could do:
$entries = Entry::find()->tags(':notempty:')->all();

Where tags is the handle of your tags field.

> **Comments:**
> **username_0**: I've used the `relatedTo` parameter when trying to find a post with a specific tag - for example `Entry::find()->relatedTo($tag)->all();` where `$tag` is a particular tag - how would I abstract this to find a post with any tag though?  
> **username_1**: Just edited my answer above.  

---

