This question is related to a previous question I asked: Sort entries in a category via entry field

I have a page that is running the same query from the previous question:

{% set businesses = craft.entries.relatedTo(category) %}
{% set state  = entry.bizTown.first.townCounty.first.countyRegion.first.regionState.first ?? 'N/A' %}
{% set businessesByTown = businesses|group('bizTown.first.townCounty.first.countyRegion.first.regionState[0].title') %}

{% if businesses|length %}
    {% for town, businessesInTown in businessesByTown %}
        <ul class="three-col-list">
                {% for business in businessesInTown %}
                <li class="business-cat__item">
                    {% if business.ytVideoId|length %}
                        <i class="fa fa-video-camera" aria-hidden="true"></i>
                    {% endif %}
                    <strong>{{ business.link }}</strong><br>

                    {% set twn = business.bizTown.first %}
                    <a class="business-cat__item--town" href="{{twn.url}}">{{twn.title}}</a>
                {% endfor %}
    {% endfor %}
{% else %} ... {% endif %}

This is on a template which is displaying entries of a given sub-category. It separates the entires (the businesses) according to its state. It also displays what town each business belongs to.

Unforunately this takes the page ~20 seconds to render! I've tried looking into using plugins for custom SQL queries, and using eager loading.

I have read through this article a lot and have poked around inside their plugin, but I still don't know where/how to start! Likewise with eager loading, with all the crap I have going on in this/these queries, I'm not sure where to start with that, either.

At this point I feel like this is abstracting beyond my comfort level, and I'd love some expert input. Thanks!

  • Is there a reason to do entry.bizTown.first.townCounty.first.countyRegion.first.regionState.[0].title again on the third line in code example instead of doing state.title? Because now you're making craft look up the same object (accross multiple relations) twice.
    – mrsmn
    Jul 25, 2017 at 5:41
  • Changing line 3 to {% set businessesByTown = businesses|group(state.title) %} results in "Impossible to access an attribute ("title") on a string variable ("N/A")." Jul 25, 2017 at 20:09


Your Answer

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

Browse other questions tagged or ask your own question.