I have a list of entries and I would like to filter them into 2 lists.

  1. Entries that have been made a relation in another section
  2. Entries that have NOT been made a relation in another section

I thought about tackling this the other way round and listing the section with the relations but some entires are related multiple times, so and the titles in the list get repeated multiple times. Unless this can filter out the duplicates, but it starts to get messy at this point.

Ideas welcome thanks.


You could use the without filter for this:

{% set entryParams = {
    section: mySection1
} %}

{% set relationParams = {
    sourceElement: mySection2,
    field: myEntriesField
} %}

{% set entriesWithRelation = craft.entries(params).relatedTo(relationParams).find() %}
{% set entriesWithoutRelation = craft.entries(params)|without(entriesWithRelation) %}
  • Without getting to the second without part, the entriesWithRelation setting with relatedTo outputs every single entry in the system regardless of section. Looking at the docs buildwithcraft.com/docs/relations I'm certain that what I'm trying to do is relatively easy, which in the first instance is: if any mySection1 entries have been made a relation in mySection2 list them here... Dec 5 '14 at 13:54
  • You probably have a lot of different kind of relations, that can make it necessary to further narrow down the query, @Simon. Will update the code...
    – carlcs
    Dec 5 '14 at 13:59
  • There are about 3 relationship fields in mySection2 entries indeed. Dec 5 '14 at 17:46
  • Did you get this to work now, @Simon? By specifying the element and the field of your relation you should probably be good!
    – carlcs
    Dec 5 '14 at 21:40
  • Got halfway there - will update later thanks. Dec 7 '14 at 7:49

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.