1

I'm looking to group entries based on a condition between two custom date fields and the current date. I'm not even sure this is possible, but this is how it would appear visually:

Happening Now

Entry 1

Entry 2

Happening in the future

Entry 3

Entry 4

Happened in the past

Entry 5

Entry 6

These entries all have two custom date fields, biddingStartDate and biddingEndDate. If the current date is between biddingStartDate and biddingEndDate, it should go under "Happening Now"; if the current date is before biddingStartDate, it should go under "Happening in the future"; and if the current date is after biddingEndDate, it should go under "Happened in the past".

The other large question here would be: how would the headings even be created since they're dynamic and not stored in the database?

1 Answer 1

2

You could fetch your entries then split them in three arrays which would give you the ability to then add headings manually and display as you need to.

{# Define today and three arrays to hold your grouped entries #}
{% set today = now|atom %}
{% set futureBids = [] %}
{% set currentBids = [] %}
{% set pastBids = [] %}

{# Fetch your entries #}
{% set entries = craft.entries().section('bids').all() %}

{# Populate your groups #}
{% for entry in entries %}
    {% set starts = entry.biddingStartDate | atom %}
    {% set ends = entry.biddingEndDate | atom %}

    {# future #}
    {% if starts > today %}
        {% set futureBids = futureBids | merge([entry]) %}

    {# current #}
    {% elseif starts <= today and ends > today %}
        {% set currentBids = currentBids | merge([entry]) %}

    {# past #}
    {% elseif ends < today %}
        {% set pastBids = pastBids | merge([entry]) %}
    {% endif %}
{% endfor %}

{# Display as you need... #}

<h2>Current Bids</h2>
<hr>
{% for entry in currentBids %}
    {{ entry.title }}<br>
{% endfor %}

<h2>Future Bids</h2>
<hr>
{% for entry in futureBids %}
    {{ entry.title }}<br>
{% endfor %}


<h2>Past Bids</h2>
<hr>
{% for entry in pastBids %}
    {{ entry.title }}<br>
{% endfor %}

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.