1

is it possible to replicate the order of related entries in the front end as they appear in the admin entries panel? (see images below)

Right now, in the front end it only sorts the order in which the entries where originally entered (sorted by entry date & time).

If not is there any other possibility to do so? Thanks

enter image description here

enter image description here

  • What does your template code for outputting this look like? If you loop through the entriy field directly, you should get the order in the entry field. – André Elvan Nov 10 '14 at 12:22
  • {% set relatedRecords = craft.entries.section('contenuDiscographie').relatedTo(entry) %} {% for record in relatedRecords %} <a href="{{ record.url }}">{{ record.title }}</a> {% for block in record.discoArtistes %} {% set relatedPerformers = craft.entries.section('interpretes').relatedTo(block) %} {% for performer in relatedPerformers %} // this needs to be sorted // {{ performer.title }} {% endfor %} {% endfor %} {% endfor %} {% endif %} – jjem Nov 10 '14 at 12:43
  • It would be best to edit your original question and add the code example there. It's difficult to read in the comments. I updated my answer to show the original and the modified code. – Douglas McDonald Nov 10 '14 at 13:10
4

The natural sort order for related entries is the order defined in the control panel (not the post date of the related entries), unless you otherwise specify the sort order. Here is an example taken from one of my projects, where related articles are defined within an entries field-type in an article entry:

{% set relatedArticles = entry.relatedArticles %}
<ul>
    {% for article in relatedArticles %}
        <li><a href="{{ article.url }}">{{ article.title }}</a></li>
    {% endfor %}
</ul>

On the backend, craft uses the table craft_relations to create the relationship with the columns sourceId, targetId, and sortOrder. This sortOrder column defines the default sort order when accessing related entries through the related 'entries' field-type.

If on the other hand, you are accessing related entries using the .relatedTo() method of the EntryCriteriaModel then you will need to specify the sort order using .order('sortOrder').

Update

Here is the original code:

{% set relatedRecords = craft.entries.section('contenuDiscographie').relatedTo(entry) %} 
{% for record in relatedRecords %} 
    <a href="{{ record.url }}">{{ record.title }}</a>    
    {% for block in record.discoArtistes %}    
        {% set relatedPerformers = craft.entries.section('interpretes').relatedTo(block) %}
        {% for performer in relatedPerformers %}     
            {{ performer.title }}
        {% endfor %}    
    {% endfor %} 
{% endfor %}

Try using the actual relationship field:

{% set relatedRecords = entry.myRelatedEntriesFieldHandle %} 
{% for record in relatedRecords %} 
    <a href="{{ record.url }}">{{ record.title }}</a>    
    {% for block in record.myMatrixBlockFieldHandle %}
        {% set relatedPerformers = block.myRelatedEntriesFieldHandleInsideMatrixField %}
        {% for performer in relatedPerformers %}      
            {{ performer.title }}
        {% endfor %}    
    {% endfor %} 
{% endfor %}

Edit I edited the code above to be more generic, rather than trying to guess what the field handles are named in your project. You need to change these to your actual field handles. From the picture above I would guess they might be 'entry.relatedRecords', 'record.discoArtistes', and 'block.nom' for example.

  • This doesn't work. {% set relatedPerformers = block.relatedPerformers %}returns an error. I tried = block.MatrixBlockName but no more success. – jjem Nov 20 '14 at 9:33
  • the error: Craft\MatrixBlockModel and its behaviors do not have a method or closure named "relatedPerformers". – jjem Nov 20 '14 at 9:36
  • You need to adapt it to use the field handle of your related entries field. So use {% set relatedPerformers = block.theFieldHandleOfMyRelatedEntriesField %} – Douglas McDonald Nov 20 '14 at 14:29
  • This was done. But it doesn't accept the Matrix field handle (discoArtistes). Also tried we the Matrix field block handle.... – jjem Nov 20 '14 at 14:59
  • There are two fieldHandles we are talking about. There is the 'matrixField handle' and there is the 'entriesField handle' inside the matrixField. What is the name of the entries field handle inside the block? – Douglas McDonald Nov 20 '14 at 15:35
2

Set the order parameter to 'sortOrder':

{% set entries = craft.entries.relatedTo(entry).order('sortOrder') %}

or do simply use the short syntax:

{% set entries = entry.entriesFieldHandle %}
  • this gives: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'sortOrder' in order clause is ambiguous..... – jjem Nov 10 '14 at 12:37
  • Does this work: {% set entries = craft.entries.relatedTo({ sourceElement: entry, field: 'entriesFieldHandle' }).order('sortOrder') %} – carlcs Nov 10 '14 at 12:42
0

After testing the above solution by Douglas

1) If the related entries are not in a Matrix block the following works perfectly. You may put the related entries in any custom order by dragging them around.

{% set relatedPerformers = entry.performer.order('sortOrder') %}

2) The related entries coming from a Matrix field gives following error: Craft\MatrixBlockModel and its behaviors do not have a method or closure named "relatedPerformers".

{% set relatedPerformers = block.performer.order('sortOrder') %}
  • 1
    You should generally use comments for this rather than posting it as an answer (or edit your original question to provide more details). You do not need to include the .order('sortOrder') when accessing the related entries through an entries field. They will be ordered by 'sortOrder' by default. – Douglas McDonald Nov 20 '14 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.