1

I was wondering if there is a better way to do a query like this, it takes a long time to execute and I know setting the parent variable is where there's a problem but am not sure of an alternative way to do it. Appreciate any and all help!

{% set searchResults = craft.entries.section(['post']).search(searchQuery).with(['catalogue', 'postImage']).where({'field_show': 1}).limit(20) %}
{% paginate searchResults as pageInfo, pageEntries %}

<div class="list">
  {% for item in pageEntries %}
        {% set image = item.postImage[0] ?? null %}
    {% set catalogue = item.catalogue[0] ?? null %}
    <a href="{{ item.url }}" class="item row {{ loop.first ? 'active' }}">
            <div class="col-8 col-sm">
              {{ item.title }}
            </div>
        </a>
    {% endfor %}
    <div class="pagination">
        <div class="row">
            <div class="col">
                {% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl }}">Previous</a>{% endif %}
            </div>
            <div class="col text-center">
                Showing page {{ pageInfo.currentPage }} of {{ pageInfo.totalPages }} pages
            </div>
            <div class="col text-right">
                {% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl }}">Next</a>{% endif %}
            </div>
        </div>
    </div>
</div>
<div class="preview">
    {% for item in pageEntries %}
      {% set image = item.postImage[0] ?? null %}
      {% set catalogue = item.catalogue[0] ?? null %}
      {% set parent = craft.entries.section('parents').relatedTo({targetElement: catalogue, field: 'catalogue'}).one() %}
        <h2>{{ item.title }}</h2>
        <p><a href="{{ parent.url }}" title="{{ parent.title }}">{{ parent.title }}</a></p>
        <p>{{ parent.date|date('l jS F Y') }} {{ parent.time and parent.archived != '1'  ? parent.time|time('g:iA') }}</p>
    {% endfor %}
</div>
3
  • I'd appreciate any input on this, can anyone see any improvements I could make?
    – Moucky
    May 11 '20 at 21:36
  • It would to know how all of this is setup. How are the parents and post sections related? Jun 22 '20 at 15:28
  • 1
    As you say, setting the parent variable on the inside of your loop is the source of the slowness as you're doing an extra set of DB queries per iteration. It's hard to give you a proper answer without knowing more details about how your sections and fields are set up, but if it were me I'd try to solve this using a hash map/array of all parents indexed by a common key which you can then match up inside the main loop without incurring the N+1 problem. Jun 23 '20 at 10:59
1

It's hard to see what you're actually trying to achieve here, but yes it's likely to do with the amount of queries you're doing on the page.

I would either re-consider the page layout (without knowing what you're trying to achieve by having two loops it's hard to judge) OR pull some, or all of the results asynchronously using javascript so the page rendering isn't blocked whilst the query is being processed.

The newly released sprig plugin may be of interest.

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.