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

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.

  • Thank you. I was so close. It works perfectly. I've also noted your edits on my post for future questions. Jul 14 '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 '14 at 17:46

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.