Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-12T11:42:50.327Z
Answers.Text: 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?
Answers.UserDisplayName: Alk
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-12T14:45:56.517Z
Answers.Text: Just edited my answer above.
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-12T11:41:12.767Z
Answers.LastActivityDate: 2019-11-12T14:45:40.633Z
Answers.LastEditDate: 2019-11-12T14:45:40.633Z
Answers.UserDisplayName: Oli
Body: <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>
  
Tags: <craft3><entry><php><tags><yii>
Title: Fetch Entries with Tags Only
ThreadText: # 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](https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#relatedto)) and [this post](https://craftcms.stackexchange.com/questions/32950/display-entries-by-tag/32963#32963) 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.  
  
  ---
  
  