1

I've tried so many different things and can't get this to work...

{% set makeCat = craft.categories()
   .group('make')
   .orderBy('title')
   .limit(21) %}

{% paginate makeCat as pageInfo, makeCars %}
{% for make in makeCars %}
{% set entries = craft.entries()
   .section('cars')
   .carStatus('Inventory')
   .relatedTo(make)
   .all() %}

    {% for entry in entries %}
       <p>{{ entry.title }}</p>
    {% endfor %}
 {% endfor %}

I'm trying to list cars with a status of 'Inventory', order them by a single category field alphabetically by car make and paginate...is this not possible in Craft? This code gets me the closest but doesn't paginate properly. I want the 60+ entries to paginate 21 per page but in order of the make of the car ie. BMW, Ferrari, Porsche, etc. Not sure what I'm missing or doing wrong but any help and direction would be super! Thanks!

1 Answer 1

4

In order to sort your entries by a related field you need to join the relations and the content table and sort your entries by the column you want. The it doesn't change the behavior of your paginate

{# the field ID of your relation field, you can as well grab it via #}
{# set field = craft.app.getFields().getFieldByHandle('carCategory').id #}
{% set fieldId = >> number << %}
{% set entries = craft
    .entries
    .section('cars')
    .leftJoin('{{%relations}} as myrelations', '[[myrelations.sourceId]] = [[elements.id]] and [[myrelations.fieldId]] = ' ~ fieldId)
    .leftJoin('{{%content}} as mycontent', '[[myrelations.targetId]] = [[mycontent.elementId]]')
    .groupBy('elements.id')
    .orderBy({'mycontent.title': SORT_ASC})
    .all()
%}

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.