3

Does anyone know how to achieve the following in Twig with Craft 3:

My sections locations and trainings.

I want to find all the entries from locations which has a relation to trainings and a datetime field value from trainings has to be after now.

1 Answer 1

4

You’d first get all the relevant entries of your “trainings” section, or better get just their IDs as that’s way better on performance. Then on another element query you set the relatedTo parameter to only get entries related to these entry IDs.

{% set trainingsEntriesIds = craft.entries({
    section: 'trainings',
    postDate: '>= ' ~ 'now'|date('c'),
}).ids() %}

{% set entries = craft.entries({
    section: 'locations',
    relatedTo: {
        targetElement: trainingsEntriesIds,
        field: 'entriesFieldHandle'
    },
}).all() %}
0

Your Answer

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

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