Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I have a page which lists assets by source. I've added a date field to the asset.

How can I order the assets by this field so that the most recent is first?

asset date field

Here is my markup:

{% set assets = craft.assets.find({sourceId: 2}) %}

{% for asset in assets %}
    <a href="{{ asset.url }}">{{ asset.date | date('M Y') }}</a>
{% endfor %}

Looking at the cheat sheet for Assets it seems that these are my only options for order: title, id, sourceId, folderId, filename, kind, width, height, size desc'.

Ideally I could do something like this:

{% set assets = craft.assets.find({sourceId: 2, order: '{{ asset.date }}'}) %}
share|improve this question

1 Answer 1

up vote 4 down vote accepted

You're so close it's maddening—you should be able to order by a custom field as well:

{% set assets = craft.assets.find({sourceId: 2, order: 'date desc'}) %}

Where date is the name of your custom field.

From the Craft docs (emphasis added):

Possible values include 'title', 'id', 'sourceId', 'folderId', 'filename', 'kind', 'width', 'height', 'size', 'dateCreated', and 'dateUpdated', as well as any textual custom field handles.

share|improve this answer
    
Thank you. I was so close. It works perfectly. I've also noted your edits on my post for future questions. –  Niall Thompson Jul 14 at 17:44
    
@NiallThompson Your question was excellent and thorough, I just did some tidying. I hope you don't mind :) –  Matt Stein Jul 14 at 17:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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