<p>The <code>relatedTo</code> parameter accepts either a single element ID or model, or an array of element IDs or models. It doesn't accept asset source handles, section handles, or a particular element type constant.</p>

<p>This means that in order to pull all tags related to a specific element type, you'll need to give the <code>relatedTo</code> parameter <em>an array of all IDs for that particular element type</em>.</p>

<p>You're not specific about what <code>travelImages</code> are, but assuming it's an Asset source, something like this should work:</p>

<pre><code>'elementType' =&gt; ElementType::Tag,
'criteria' =&gt; [
    'relatedTo' =&gt; craft()-&gt;elements-&gt;getCriteria(ElementType::Asset, [
        'limit' =&gt; null,
    ])-&gt;ids(),
    'limit' =&gt; null,
],
</code></pre>

<p>Note that if <code>travelImages</code> isn't an Asset source, you'll need to change <code>ElementType::Asset</code> to <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/enums/ElementType.php#L21" rel="nofollow">the appropriate element type</a> (e.g. <code>ElementType::Entry</code>, <code>ElementType::Category</code> etc).</p>

CC BY-SA 3.0
2016-10-01T19:24:58.663Z
Doesn't work. Got an exception:  


`CException

Craft\WebApp and its behaviors do not have a method or closure named "entries".`
Siggi Foster
CC BY-SA 3.0
2016-10-02T07:07:01.407Z
@SiggiFoster Sorry about that – I was thinking in Twig syntax :) See updated answer above.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-10-04T18:46:12.087Z
Works perfect. You were right. I forgot to mention what type of element travelImages are. It's an entry element type. Changed it to `ElementType::Entry` and problem solved.  Thank you very much.
Siggi Foster
CC BY-SA 3.0
2016-09-15T00:43:08.107Z
2016-10-02T17:43:03.550Z
2016-10-02T17:43:03.550Z
Mats Mikkel Rummelhoff
<p>For anyone trying to do this with Craft 5+ the syntax has changed a little.</p>
<p>You now need to import the <code>Entry</code> function, and call that:</p>
<pre><code>use craft\elements\Entry;

...

'elementType' =&gt; Category::class,
'criteria' =&gt; ['group' =&gt; 'author', 'orderBy' =&gt; 'title ASC', 'limit' =&gt; 100, 'relatedTo' =&gt; Entry::find()-&gt;type(['article'])-&gt;all()],
</code></pre>

CC BY-SA 4.0
2024-10-24T15:37:54.590Z
2024-10-24T15:37:54.590Z
theAdhocracy
<p>I tried to get all tags that are related to an section.
Everything I tried failed. Is there a way to get all assigned tags for a particular element type by using element criteria model.
In this case <code>travelImages</code>? I want to avoid to display not related tags.</p>

<pre><code>'elementType' =&gt; 'Tag',
'criteria' =&gt; ['relatedto' =&gt; 'travelImages']
</code></pre>

<elementcriteriamodel><plugin-elementapi>
In the Element API plugin, how can I get only tags that are related to a particular section?
# In the Element API plugin, how can I get only tags that are related to a particular section?

(Asked by: username_0 on 2016-09-14)

I tried to get all tags that are related to an section.
Everything I tried failed. Is there a way to get all assigned tags for a particular element type by using element criteria model.
In this case travelImages? I want to avoid to display not related tags.
'elementType' => 'Tag',
'criteria' => ['relatedto' => 'travelImages']

**Tags:** elementcriteriamodel, plugin-elementapi

---

## 2 answers

### Answer by username_1 on 2016-09-15. Score: 3 | Accepted answer

The relatedTo parameter accepts either a single element ID or model, or an array of element IDs or models. It doesn't accept asset source handles, section handles, or a particular element type constant.
This means that in order to pull all tags related to a specific element type, you'll need to give the relatedTo parameter an array of all IDs for that particular element type.
You're not specific about what travelImages are, but assuming it's an Asset source, something like this should work:
'elementType' => ElementType::Tag,
'criteria' => [
    'relatedTo' => craft()->elements->getCriteria(ElementType::Asset, [
        'limit' => null,
    ])->ids(),
    'limit' => null,
],

Note that if travelImages isn't an Asset source, you'll need to change ElementType::Asset to the appropriate element type (e.g. ElementType::Entry, ElementType::Category etc).

> **Comments:**
> **username_0**: Doesn't work. Got an exception:     `CException  Craft\WebApp and its behaviors do not have a method or closure named "entries".`  
> **username_1**: @SiggiFoster Sorry about that – I was thinking in Twig syntax :) See updated answer above.  
> **username_0**: Works perfect. You were right. I forgot to mention what type of element travelImages are. It's an entry element type. Changed it to `ElementType::Entry` and problem solved.  Thank you very much.  

---

### Answer by username_2 on 2024-10-24. Score: 0

For anyone trying to do this with Craft 5+ the syntax has changed a little.
You now need to import the Entry function, and call that:
use craft\elements\Entry;

...

'elementType' => Category::class,
'criteria' => ['group' => 'author', 'orderBy' => 'title ASC', 'limit' => 100, 'relatedTo' => Entry::find()->type(['article'])->all()],


---

