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.

1 Answer 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

  • 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
    Mar 18, 2015 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.