<p>The <code>targetElement</code> and <code>sourceElement</code> take either an ID, an Element Model, or an array of either.</p>

<p>So, in your example, you'd want to do:</p>

<pre><code>{# Categories #}
{% set CatToDo = craft.categories.slug('things-to-do').first %}
{% set CatToEat = craft.categories.slug('things-to-eat').first %}

{% set toEat = craft.entries.section('yourSection').relatedTo({
   targetElement: CatToEat,
   field: 'activityPicker'
}) %}

[...]
</code></pre>

<p>Notice the <strong>.first</strong> in there as the difference. You will only want the one category since you're passing the slug.</p>

<p>Note: This assumes that <code>CatToEat</code> is indeed the targetElement. See here for more information: <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow">http://buildwithcraft.com/docs/relations#the-relatedTo-param</a></p>

CC BY-SA 3.0
2015-03-18T04:00:20.660Z
Thanks Peter, however that still didn't work out as it returned all entries regardless of whether they were selected or not. I'm going down another root, which is to "deconstruct" the entries field and add an `and` criteria to the `relatedTo` param. Works well however i'm having trouble with the `sortOrder`: 
    {% set entriesDo = craft.entries({
    relatedTo: ['and',
    { sourceElement: entry, field: "activityPicker" },
    { targetElement: catDo }
    ],
    order: "sortOrder",
    limit: null
    }) %}
Paul
CC BY-SA 3.0
2015-03-17T14:26:37.287Z
2015-03-17T14:26:37.287Z
Peter Tell
<p>I have an entries field (<code>activityPicker</code>) that links to other entries in a different section. These other entries have categories (<code>things-to-do</code> and <code>things-to-eat</code>), and I'm trying to figure out how to filter these entries by categories.</p>

<p>My first instinct was to do something similar to this:</p>

<pre><code>{# Categories #}
{% set CatToDo = craft.categories.slug('things-to-do') %}
{% set CatToEat = craft.categories.slug('things-to-eat') %}

{# Entries #}    
{% set toDo = entry.activityPicker.relatedTo(catToDo) %}
{% set toEat = entry.activityPicker.relatedTo(catToEat) %}

{# Output #}
{% for item in toDo %}
   {{ item }}
{% endfor %}

{% for item in toEat %}
   {{ item }}
{% endfor %}
</code></pre>

<p>however that didn't work so I tried playing with <code>targetElement</code> and <code>sourceElement</code> in the <code>relatedTo</code> parameter:</p>

<pre><code>{% set toEat = entry.activityPicker.relatedTo({
   targetElement: catToEat
}) %}

[...]
</code></pre>

<p>but this still doesn't work. Basically I get that <code>entry.activityPicker</code> returns an array of posts, but I haven't figured out how to filter those posts in a meaningful way before they hit the template.</p>

<categories><entries-field>
Entries field: Output entries related to category
# Entries field: Output entries related to category

(Asked by: username_0 on 2015-03-17)

I have an entries field (activityPicker) that links to other entries in a different section. These other entries have categories (things-to-do and things-to-eat), and I'm trying to figure out how to filter these entries by categories.
My first instinct was to do something similar to this:
{# Categories #}
{% set CatToDo = craft.categories.slug('things-to-do') %}
{% set CatToEat = craft.categories.slug('things-to-eat') %}

{# Entries #}    
{% set toDo = entry.activityPicker.relatedTo(catToDo) %}
{% set toEat = entry.activityPicker.relatedTo(catToEat) %}

{# Output #}
{% for item in toDo %}
   {{ item }}
{% endfor %}

{% for item in toEat %}
   {{ item }}
{% endfor %}

however that didn't work so I tried playing with targetElement and sourceElement in the relatedTo parameter:
{% set toEat = entry.activityPicker.relatedTo({
   targetElement: catToEat
}) %}

[...]

but this still doesn't work. Basically I get that entry.activityPicker returns an array of posts, but I haven't figured out how to filter those posts in a meaningful way before they hit the template.

**Tags:** categories, entries-field


---

## 1 answers

### Answer by username_1 on 2015-03-17. Score: 1

The targetElement and sourceElement take either an ID, an Element Model, or an array of either.
So, in your example, you'd want to do:
{# Categories #}
{% set CatToDo = craft.categories.slug('things-to-do').first %}
{% set CatToEat = craft.categories.slug('things-to-eat').first %}

{% set toEat = craft.entries.section('yourSection').relatedTo({
   targetElement: CatToEat,
   field: 'activityPicker'
}) %}

[...]

Notice the .first in there as the difference. You will only want the one category since you're passing the slug.
Note: This assumes that CatToEat is indeed the targetElement. See here for more information: http://buildwithcraft.com/docs/relations#the-relatedTo-param

> **Comments:**
> **username_0**: Thanks Peter, however that still didn't work out as it returned all entries regardless of whether they were selected or not. I'm going down another root, which is to "deconstruct" the entries field and add an `and` criteria to the `relatedTo` param. Works well however i'm having trouble with the `sortOrder`:      {% set entriesDo = craft.entries({     relatedTo: ['and',     { sourceElement: entry, field: "activityPicker" },     { targetElement: catDo }     ],     order: "sortOrder",     limit: null     }) %}  

---

