Answers.Body: <p>First you’ll need to get the Table Of Contents entry that holds the Entries field (the <strong>source entry</strong>). I’m going to assume that is in a Single section with the handle <code>toc</code>.</p>
  
  <pre><code>{% set toc = craft.entries.section('toc').first() %}
  </code></pre>
  
  <p>Once you have that, you can just pass its Entries field value into getPrev() and getNext():</p>
  
  <pre><code>{% set prevEntry = entry.getPrev(toc.myEntriesField) %}
  {% set nextEntry = entry.getNext(toc.myEntriesField) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-17T19:21:49.997Z
Answers.Text: Thanks for the quick reply, Brandon! The requirements changed a bit and I posed an updated question here: http://craftcms.stackexchange.com/q/15074/187
Answers.UserDisplayName: Mark J. Reeves
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-17T15:44:53.977Z
Answers.LastActivityDate: 2016-05-17T15:44:53.977Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>There are a few tickets touching on this, but the solutions proposed aren't working and I'd like to share the use case:</p>
  
  <p>I have a Table of Contents page that is outputting a set of entries based on an Entries field, in the order they appear in that field. When you click on an entry on that page, it loads on its own page.</p>
  
  <p>At the bottom of the individual entry page, I have Prev/Next URLs, using:</p>
  
  <pre><code>{% set data = craft.entries.section('works').relatedTo(issue.contents).limit(null) %}
  {% set prevEntry = entry.getPrev(data) %}
  {% set nextEntry = entry.getNext(data) %}
  </code></pre>
  
  <p>The prev &amp; next entries there, though, are based on a sort in the 'works' section, not the entries field.</p>
  
  <p>I've tried this, per <a href="https://craftcms.com/docs/entries-fields#templating" rel="nofollow">the documentation</a>:</p>
  
  <pre><code>{% set entries = craft.entries(
      {
          relatedTo: { sourceElement: issue, field: "contents" },
          order:     "sortOrder",
          limit:     null
      }
  ) %}
  </code></pre>
  
  <p>That gives me the following error. How can I sort <code>.relatedTo</code> entries based on the source field's order?</p>
  
  <pre><code>Integrity constraint violation: 1052 Column 'sortOrder' in order clause is ambiguous. The SQL statement executed was: SELECT `elements`.`id`, `entries`.`sectionId`, `entries`.`typeId`, `entries`.`authorId`, `entries`.`postDate`, `entries`.`expiryDate`
  FROM `craft_elements` `elements`
  JOIN `craft_elements_i18n` `elements_i18n` ON elements_i18n.elementId = elements.id
  JOIN `craft_content` `content` ON content.elementId = elements.id
  LEFT JOIN `craft_relations` `sources1` ON (sources1.targetId = elements.id)
  LEFT JOIN `craft_matrixblocks` `target_matrixblocks1` ON target_matrixblocks1.id = sources1.sourceId
  JOIN `craft_entries` `entries` ON entries.id = elements.id
  JOIN `craft_sections` `sections` ON sections.id = entries.sectionId
  LEFT JOIN `craft_structures` `structures` ON structures.id = sections.structureId
  LEFT JOIN `craft_structureelements` `structureelements` ON (structureelements.structureId = structures.id) AND (structureelements.elementId = entries.id)
  WHERE (((((elements_i18n.locale = :locale) AND (content.locale = :locale)) AND (elements.archived = 0)) AND ((elements.enabled = 1) AND (elements_i18n.enabled = 1) AND (entries.postDate &lt;= '2016-05-17 14:08:35') AND ((entries.expiryDate is null) OR (entries.expiryDate &gt; '2016-05-17 14:08:35')))) AND (elements_i18n.enabled = 1)) AND ((target_matrixblocks1.ownerId=:target_matrixblocks1ownerId1) AND (target_matrixblocks1.fieldId = 15))
  GROUP BY `elements`.`id`
  ORDER BY `sortOrder`. Bound with :locale='en_us', :target_matrixblocks1ownerId1='6'
  </code></pre>
  
Tags: <relations><entries-field><ordering>
Title: Ordering relatedTo entries
ThreadText: # Ordering relatedTo entries
  
  (Asked by: username_0 on 2016-05-17)
  
  There are a few tickets touching on this, but the solutions proposed aren't working and I'd like to share the use case:
  
  I have a Table of Contents page that is outputting a set of entries based on an Entries field, in the order they appear in that field. When you click on an entry on that page, it loads on its own page.
  
  At the bottom of the individual entry page, I have Prev/Next URLs, using:
  
  ```
  {% set data = craft.entries.section('works').relatedTo(issue.contents).limit(null) %}
  {% set prevEntry = entry.getPrev(data) %}
  {% set nextEntry = entry.getNext(data) %}
  
  ```
  
  The prev & next entries there, though, are based on a sort in the 'works' section, not the entries field.
  
  I've tried this, per [the documentation](https://craftcms.com/docs/entries-fields#templating):
  
  ```
  {% set entries = craft.entries(
      {
          relatedTo: { sourceElement: issue, field: "contents" },
          order:     "sortOrder",
          limit:     null
      }
  ) %}
  
  ```
  
  That gives me the following error. How can I sort `.relatedTo` entries based on the source field's order?
  
  ```
  Integrity constraint violation: 1052 Column 'sortOrder' in order clause is ambiguous. The SQL statement executed was: SELECT `elements`.`id`, `entries`.`sectionId`, `entries`.`typeId`, `entries`.`authorId`, `entries`.`postDate`, `entries`.`expiryDate`
  FROM `craft_elements` `elements`
  JOIN `craft_elements_i18n` `elements_i18n` ON elements_i18n.elementId = elements.id
  JOIN `craft_content` `content` ON content.elementId = elements.id
  LEFT JOIN `craft_relations` `sources1` ON (sources1.targetId = elements.id)
  LEFT JOIN `craft_matrixblocks` `target_matrixblocks1` ON target_matrixblocks1.id = sources1.sourceId
  JOIN `craft_entries` `entries` ON entries.id = elements.id
  JOIN `craft_sections` `sections` ON sections.id = entries.sectionId
  LEFT JOIN `craft_structures` `structures` ON structures.id = sections.structureId
  LEFT JOIN `craft_structureelements` `structureelements` ON (structureelements.structureId = structures.id) AND (structureelements.elementId = entries.id)
  WHERE (((((elements_i18n.locale = :locale) AND (content.locale = :locale)) AND (elements.archived = 0)) AND ((elements.enabled = 1) AND (elements_i18n.enabled = 1) AND (entries.postDate <= '2016-05-17 14:08:35') AND ((entries.expiryDate is null) OR (entries.expiryDate > '2016-05-17 14:08:35')))) AND (elements_i18n.enabled = 1)) AND ((target_matrixblocks1.ownerId=:target_matrixblocks1ownerId1) AND (target_matrixblocks1.fieldId = 15))
  GROUP BY `elements`.`id`
  ORDER BY `sortOrder`. Bound with :locale='en_us', :target_matrixblocks1ownerId1='6'
  
  ```
  
  **Tags:** relations, entries-field, ordering
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-05-17. Score: 1 | Accepted answer
  
  First you’ll need to get the Table Of Contents entry that holds the Entries field (the **source entry**). I’m going to assume that is in a Single section with the handle `toc`.
  
  ```
  {% set toc = craft.entries.section('toc').first() %}
  
  ```
  
  Once you have that, you can just pass its Entries field value into getPrev() and getNext():
  
  ```
  {% set prevEntry = entry.getPrev(toc.myEntriesField) %}
  {% set nextEntry = entry.getNext(toc.myEntriesField) %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for the quick reply, username_1! The requirements changed a bit and I posed an updated question here: http://craftcms.stackexchange.com/q/15074/187  
  
  ---
  
  