Answers.Body: <p>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</p>
  
  <pre class="lang-twig prettyprint-override"><code>{# the field ID of your relation field, you can as well grab it via #}
  {# set field = craft.app.getFields().getFieldByHandle('carCategory').id #}
  {% set fieldId = &gt;&gt; number &lt;&lt; %}
  {% 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()
  %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-05T06:11:51.280Z
Answers.LastActivityDate: 2019-04-05T06:11:51.280Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I've tried so many different things and can't get this to work...</p>
  
  <pre><code>{% 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 %}
         &lt;p&gt;{{ entry.title }}&lt;/p&gt;
      {% endfor %}
   {% endfor %}
  </code></pre>
  
  <p>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!</p>
  
Tags: <craft3><categories><pagination><ordering><relatedto>
Title: Order entries by a category field alphabetically then paginate
ThreadText: # Order entries by a category field alphabetically then paginate
  
  (Asked by: username_0 on 2019-04-05)
  
  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!
  
  **Tags:** craft3, categories, pagination, ordering, relatedto
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-04-05. Score: 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
  
  ```twig
  {# 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()
  %}
  
  ```
  
  
  ---
  
  