1

I have quite a substantial amount of queries heading out for this bit of code below. I've tried quite a few things to get it down, but I need to eager load some things just to try and reduce the overhead of this, but I still think it won't do enough. Is there anything I can look at to optimise this?

    {% set brandIds = craft.entries().section('brands').relatedTo({ targetElement: category }).ids() %}
    {% set offers = craft.entries().section('offers').relatedTo(brandIds).limit(6) %}

    {% for entry in offers %}
      <div class="card category-card">
        <a href="{{ entry.relatedBrand.one.url }}#{{ entry.id }}" title="{{ entry.title }}">
        <div class="row">
          <div class="col-md-12 col-4">
              {% for asset in entry.relatedBrand.one.brandLogo.all() %}
                <img class="card-img-top" src="{{ asset.geturl }}" alt="Card image cap">
              {% endfor %}
          </div>
          <div class="col-md-12 col-8">
            <div class="card-body">
              <h5 class="card-title">{{ entry.title }}</h5>
              <p>{{ entry.body }}</p>
              <small class="text-muted">{{ entry.relatedBrand.one }}</small>
            </div>
          </div>
          </div>
        </a>
      </div>
  {% endfor %}
2

First of all just a few ideas about best practices

Don't

{{ entry.relatedBrand.one.url }}
{{ entry.relatedBrand.one.title }}
{{ entry.relatedBrand.one.size }}

Do

{% set relatedImage = entry.relatedBrand.one() %}
{{ relatedImage.url }}
{{ relatedImage.title }}
{{ relatedImage.size }}

Furthermore your code with eager loading

{% set brandIds = craft
    .entries()
    .section('brands')
    .relatedTo({ targetElement: category })
    .ids() 
%}
{% set offers = craft
    .entries()
    .section('offers')
    .with(['relatedBrand.brandLogo'])
    .relatedTo(brandIds)
    .limit(6) 
    .all()
%}

{% for entry in offers %}
  <div class="card category-card">
    {% set relatedBrand = entry.relatedBrand? entry.relatedBrand[0] : null %}
    <a href="{{ relatedBrand.url?? '' }}#{{ entry.id }}" title="{{ entry.title }}">
    <div class="row">
      <div class="col-md-12 col-4">
          {% set logos = relatedBrand? relatedBrand.brandLogo : [] %}
          {% for asset in logos %}
            <img class="card-img-top" src="{{ asset.geturl }}" alt="Card image cap">
          {% endfor %}
      </div>
      <div class="col-md-12 col-8">
        <div class="card-body">
          <h5 class="card-title">{{ entry.title }}</h5>
          <p>{{ entry.body }}</p>
          <small class="text-muted">{{ relatedBrand.title?? '' }}</small>
        </div>
      </div>
      </div>
    </a>
  </div>
{% endfor %}
| improve this answer | |
  • Robin! You're amazing! I literally tried 15 things on how to increase the speed of this!!! This has literally nearly halved the load on that query! – JMKelley Oct 29 '18 at 14:28

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.