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:

{% 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'

1 Answer 1


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) %}

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.