Answers.Body: <p>You can get all of the <code>works</code> entries related to your <code>contents</code> Matrix field like so:</p>
  
  <pre><code>{% set works = craft.entries({
      relatedTo: { sourceElement: issue, field: 'contents.works' }
  }) %}
  </code></pre>
  
  <p>They will most likely be out of order though. We can control that with the <code>order</code> parameter.</p>
  
  <p>If you enable Dev Mode and look at the resulting SQL query in your browser console, you will see these two tables getting joined into it:</p>
  
  <pre class="lang-sql prettyprint-override"><code>LEFT JOIN `craft_relations` `sources1` ON (sources1.targetId = elements.id)
  LEFT JOIN `craft_matrixblocks` `target_matrixblocks1` ON target_matrixblocks1.id = sources1.sourceId
  </code></pre>
  
  <p>So we can pass in an <code>order</code> param that tells MySQL to order the entries by the block order first and their relation order second:</p>
  
  <pre><code>{% set works = craft.entries({
      relatedTo: { sourceElement: issue, field: 'contents.works' },
      order: 'target_matrixblocks1.sortOrder, sources1.sortOrder'
  }) %}
  </code></pre>
  
  <p>Now you’ve got all of the Works entries in the proper order, and you can pass them into getPrev() and getNext():</p>
  
  <pre><code>{% set prevEntry = entry.getPrev(works) %}
  {% set nextEntry = entry.getNext(works) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-18T13:26:34.763Z
Answers.Text: Yes! Thank you! I swapped `lastName` for `sources1.sortOrder` so that they're sorted by author last name within each block. Works great.
Answers.UserDisplayName: Mark J. Reeves
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-17T20:52:12.443Z
Answers.LastActivityDate: 2016-05-17T20:52:12.443Z
Answers.UserDisplayName: Brandon Kelly
Body: <p><code>issue</code> is an entry that has a matrix field named <code>contents</code>, whose blocks have an entries field named <code>works</code>, and the related <code>works</code> channel has an entries field named <code>authorPeople</code>.</p>
  
  <p>Issue 1's <code>contents</code> consists of 2 blocks: A 'regular' section and an 'alumni' section. On the cover page, I loop through the blocks. For each block, I get the entries related to their authors, so that I'm outputting entries sorted by author last name, where some authors have more than one entry associated:</p>
  
  <pre><code>{% set contributors = craft.entries({
      section: 'contributors',
      relatedTo: { sourceElement: block.works, field: "authorPeople" }
  }) %}
  
  {% set articles = craft.entries({
      section: 'works',
      relatedTo: contributors
      order: 'lastName'
  }) %}
  </code></pre>
  
  <p>That works fine, because I've got access to <code>block.works</code>. Any optimization tips are welcome :)</p>
  
  <p>On the individual entry page, I want to <code>getPrev</code> and <code>getNext</code> entries spanning both blocks. So if you're at the last entry in the 'regular' section, your next link is the first entry in the 'alumni' section. And if you're on the first entry in the 'alumni' section, your prev link is the last entry in the 'regular' section. The last entry in the last section won't return a next link, so I can code in a link to another page, and the first entry in the first section won't return a previous link, so I can code in a link to the cover page.</p>
  
  <p>Without having to loop through the blocks, how can I get that? Trying to order by <code>issue.contents</code> doesn't seem to work.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-17T19:34:59.840Z
Comments.Text: By “individual entry page” you mean for a Works entry, right? Considering it’s possible to relate the same Works entry from multiple Issues entries, how does the Works entry know which Issues entry the navigation should be scoped to?
Comments.UserDisplayName: Brandon Kelly
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-17T20:20:34.603Z
Comments.Text: That's right. I've got a handle on that here: `{% set issue = craft.entries.section('issues').slug( craft.request.getSegment(2) ).first() %}`. That's what I'm attempting to use for `issue.contents`.
Comments.UserDisplayName: Mark J. Reeves
Tags: <matrix><relations><ordering><sorting><previous-next>
Title: Sorting across blocks (to get prev/next links)
ThreadText: # Sorting across blocks (to get prev/next links)
  
  (Asked by: username_0 on 2016-05-17)
  
  `issue` is an entry that has a matrix field named `contents`, whose blocks have an entries field named `works`, and the related `works` channel has an entries field named `authorPeople`.
  
  Issue 1's `contents` consists of 2 blocks: A 'regular' section and an 'alumni' section. On the cover page, I loop through the blocks. For each block, I get the entries related to their authors, so that I'm outputting entries sorted by author last name, where some authors have more than one entry associated:
  
  ```
  {% set contributors = craft.entries({
      section: 'contributors',
      relatedTo: { sourceElement: block.works, field: "authorPeople" }
  }) %}
  
  {% set articles = craft.entries({
      section: 'works',
      relatedTo: contributors
      order: 'lastName'
  }) %}
  
  ```
  
  That works fine, because I've got access to `block.works`. Any optimization tips are welcome :)
  
  On the individual entry page, I want to `getPrev` and `getNext` entries spanning both blocks. So if you're at the last entry in the 'regular' section, your next link is the first entry in the 'alumni' section. And if you're on the first entry in the 'alumni' section, your prev link is the last entry in the 'regular' section. The last entry in the last section won't return a next link, so I can code in a link to another page, and the first entry in the first section won't return a previous link, so I can code in a link to the cover page.
  
  Without having to loop through the blocks, how can I get that? Trying to order by `issue.contents` doesn't seem to work.
  
  **Tags:** matrix, relations, ordering, sorting, previous-next
  
  > **Comments:**
  > **username_1**: By “individual entry page” you mean for a Works entry, right? Considering it’s possible to relate the same Works entry from multiple Issues entries, how does the Works entry know which Issues entry the navigation should be scoped to?  
  > **username_0**: That's right. I've got a handle on that here: `{% set issue = craft.entries.section('issues').slug( craft.request.getSegment(2) ).first() %}`. That's what I'm attempting to use for `issue.contents`.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-05-17. Score: 3 | Accepted answer
  
  You can get all of the `works` entries related to your `contents` Matrix field like so:
  
  ```
  {% set works = craft.entries({
      relatedTo: { sourceElement: issue, field: 'contents.works' }
  }) %}
  
  ```
  
  They will most likely be out of order though. We can control that with the `order` parameter.
  
  If you enable Dev Mode and look at the resulting SQL query in your browser console, you will see these two tables getting joined into it:
  
  ```sql
  LEFT JOIN `craft_relations` `sources1` ON (sources1.targetId = elements.id)
  LEFT JOIN `craft_matrixblocks` `target_matrixblocks1` ON target_matrixblocks1.id = sources1.sourceId
  
  ```
  
  So we can pass in an `order` param that tells MySQL to order the entries by the block order first and their relation order second:
  
  ```
  {% set works = craft.entries({
      relatedTo: { sourceElement: issue, field: 'contents.works' },
      order: 'target_matrixblocks1.sortOrder, sources1.sortOrder'
  }) %}
  
  ```
  
  Now you’ve got all of the Works entries in the proper order, and you can pass them into getPrev() and getNext():
  
  ```
  {% set prevEntry = entry.getPrev(works) %}
  {% set nextEntry = entry.getNext(works) %}
  
  ```
  
  > **Comments:**
  > **username_0**: Yes! Thank you! I swapped `lastName` for `sources1.sortOrder` so that they're sorted by author last name within each block. Works great.  
  
  ---
  
  