0

I have a channel of entries that are being pulled into a single. I then want to group the entries by a dropdown field called dealStatus.

This is working correctly, however the grouping is not outputting the order correctly. I expect the order of the groups to be in the same order that the dropdown field has them.

InMarket postBid closing closed

Instead the groups are getting output in this order

postBid inMarket closing closed

How do I get the groups ordered the same as the dropdown field option order?

My current code:

{% set allDeals = craft.entries.section('deals').relatedTo(entry) %}
{% set allDealsByStatus = allDeals|group('dealStatus') %}

{% for status, entriesInStatus in allDealsByStatus %}
    <li class="accordion-item {% if loop.index =="1" %}is-active{% endif %}" data-accordion-item>
      <!-- Accordion tab title -->
      <a href="#" class="accordion-title">
      {{ status }}
      <i class="arrow right"></i><!-- /.arrow right -->
      </a>

      <!-- Accordion tab content: it would start in the open state due to using the `is-active` state class. -->
      <div class="accordion-content" data-tab-content>
        <ul class="no-bullet market-content">
          {% for entry in entriesInStatus %}
            <li>
              <a href="">
                <img src="http://via.placeholder.com/120x60/eaeaea?text=logo here" alt="" class="market-logo">

                <p>{{ entry.title }}</p>

                <img src="/assets/img/next.svg" alt="" class="go-icon">
              </a>
            </li>
          {% endfor %}
        </ul><!-- /.no-bullet -->
      </div>
    </li>
{% endfor %}

This is on Craft 3.0.1

1

The group filter loops through the passed elements in the order that they are returned by your query and builds the groups array in the order that it encounters the values being grouped. So there’s no predictable way to control what order the array keys are ordered.

If you want to loop through all entries by dealStatus in a fixed order you could replace your for loop with:

{% for status in [‘InMarket’,’postBid’,’closing’,’closed’] %}
    {% if allDealsByStatus[status] is defined %}

        {% set entriesInStatus = allDealsByStatus[status] %}

        {# your list output code here #}

    {% endif %}
{% endfor %}

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.