1

The setup:

Section A (entries I want to list)

Section B

  • entries field (related to 1 entry from SectionA)

My goal is to list all entries from Section A, that are related to an entry in Section B authored by the currentUser.

Right now what I'm doing is looping through all entries in Section B authored by the current user and querying/displaying the entry related to it, but it's way too inefficient and eats up a lot of database queries. Was hoping there's a more efficient way of doing this.

Not sure whether or not there's a way to achieve this sort of relation via a complex relatedTo or if I need to go another route.

Can provide additional information/clarification if anything is unclear.

Thanks

1 Answer 1

8

I think this is what you mean:

{% set sectionBEntries = craft.entries.section('B').authorId(currentUser.id) %}

{# 'relatedTo' works both ways, so we can use sectionBEntries 
    as input for relatedTo to get all section A entries that have 
    a relation to one of the section B entries by currentUser #}
{% set sectionAEntries = craft.entries.section('A').relatedTo(sectionBEntries) %}

{% for entry in sectionAEntries %}
    {{ entry.title }}
{% endfor %}
1
  • 1
    That's exactly what I'm looking for. Had no idea that you could use groups of entries in relatedTo like that, 'doh. Many thanks!
    – Graham
    Commented Nov 27, 2014 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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