1

I've got a front end form where a user can select multiple months to find entries with dates in those months.

I had code that does that for one month, but now I need to expand that to multiple months.

{# craft.app.request.getParam('departure') format is going to yyyy-mm-dd #}

{% if craft.app.request.getParam('departure') | length %}
    {% set lastDay = craft.app.request.getParam('departure') [0:7] %}
    {% set lastDay = lastDay ~ '-31' %}
    {% set firstDay = craft.app.request.getParam('departure') %}
    {% set entries = entries.startsOn(['and', '>= '~firstDay, '< '~ lastDay]) %}
{% endif %}

I was trying to combine it as I have with other fields like so:

 {% set deptDates = [] %}
    {% set deptDates = deptDates|merge(['or']) %}
    {% for dept in craft.app.request.getParam('departure') %}

        {% set lastDay = dept [0:7] %}
        {% set lastDay = lastDay ~ '-31' %}
        {% set firstDay = dept %}
        {% set deptDates = deptDates|merge(['and', '>= '~firstDay, '< '~ lastDay]) %}

    {% endfor %}
    {% set entries = entries.startsOn(deptDates) %}

But, for obvious reasons, this didn't work :)

Any ideas gratefully received!

2

What an idiot.

I almost never use Advanced element queries which is why it never popped into my head I guess.

So you can add where() and orWhere() into your standard craft.entries calls.

So my code becomes

{% if craft.app.request.getParam('departure') | length %}
    {% for dept in craft.app.request.getParam('departure') %}
        {% set lastDay = dept [0:7] %}
        {% set lastDay = lastDay ~ '-31' %}
        {% set firstDay = dept %}
        {% set entries = entries.orWhere(
            ['between', 'field_startsOn', "#{firstDay}", "#{lastDay}" ]
        ) %}
    {% endfor %}
{% endif %}

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.