<p>The <a href="http://buildwithcraft.com/docs/templating/craft.entries#order">order param</a> sets the <code>ORDER BY</code> part of the MySQL query. You can also pass MySQL functions in it.</p>

<p>In your case <a href="http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_greatest"><code>GREATEST()</code></a> should do the job:</p>

<pre><code>{% set allEntries = craft.entries
    .section(['publication','text', 'exhibition'])
    .order('GREATEST(releaseDate, publicationReleaseDate, exhibitionOpeningDate) desc')
    .limit(null) %}
</code></pre>

CC BY-SA 3.0
2014-06-12T18:26:13.093Z
This totally should work, except that there’s a bug in Craft that is preventing it. Just fixed it for the next release. In the meantime, you will just need to add “field_” to the beginning of the first field handle in that `GREATEST()` list, e.g. `GREATEST(field_releaseDate, publicationReleaseDate, exhibitionOpeningDate)`. (The next update won’t break that, just make it not necessary.)
Brandon Kelly
CC BY-SA 3.0
2014-06-12T20:55:59.280Z
Thanks @Brandon, already noticed that bug and added "field_" to all field handles.
carlcs
CC BY-SA 3.0
2014-06-12T21:01:44.603Z
As I had already merged all those date fields into one before asking this question, I can't say yet if this code is indeed working. Will do some testing tomorrow.
carlcs
CC BY-SA 3.0
2014-06-14T21:42:48.527Z
By "merged" I meant that I copied the content from the separate fields into one single field, that I now use in all channels. Didn't come around to do the testing yet. Sorry for delaying the answer accept.
carlcs
CC BY-SA 3.0
2014-06-14T21:43:11.653Z
@Brandon I could as well stick to a single field in all channels, but then I'd like to give this field a slightly different name and different instructions in the individual channels. Currently not possible, is it?
carlcs
CC BY-SA 3.0
2014-06-14T21:46:10.740Z
@ChristianSeelbach Nope that’s not currently possible. This use case makes a good argument for it, though.
Brandon Kelly
CC BY-SA 4.0
2023-02-09T15:29:27.027Z
I realize this questions is almost a decade old, but is this still supported in Craft 4? When I try this...

`{% for entry in craft.entries.section(['articles','podcasts']).orderby('GREATEST(postDate, featuredDate) desc').all() %}` I get a SQL error.
BrianM
CC BY-SA 3.0
2014-06-12T16:10:56.020Z
2014-06-12T18:32:22.243Z
2014-06-12T18:32:22.243Z
Victor
<p>This is how I query elements from multiple channels:</p>

<pre><code>{% set allEntries = craft.entries.section(['publication','text', 'exhibition']).limit(null) %}
</code></pre>

<p>I now want to sort all these entries by a custom date field. If all channels had the same date field, I would just append <code>order('releaseDate desc')</code> to the query.</p>

<p>But as I have chosen to set up individual date fields (i.e. <code>publicationReleaseDate</code>, <code>exhibitionOpeningDate</code>) I cannot figure out how to join and sort them.</p>

<p>Of course I could just use the same field in all channels, but then I can't have individual field names and instructions per channel.</p>

<elementcriteriamodel><join><sorting>
How do you sort elements queried from multiple channels?
# How do you sort elements queried from multiple channels?

(Asked by: username_0 on 2014-06-12)

This is how I query elements from multiple channels:
{% set allEntries = craft.entries.section(['publication','text', 'exhibition']).limit(null) %}

I now want to sort all these entries by a custom date field. If all channels had the same date field, I would just append order('releaseDate desc') to the query.
But as I have chosen to set up individual date fields (i.e. publicationReleaseDate, exhibitionOpeningDate) I cannot figure out how to join and sort them.
Of course I could just use the same field in all channels, but then I can't have individual field names and instructions per channel.

**Tags:** elementcriteriamodel, join, sorting


---

## 1 answers

### Answer by username_1 on 2014-06-12. Score: 9 | Accepted answer

The order param sets the ORDER BY part of the MySQL query. You can also pass MySQL functions in it.
In your case GREATEST() should do the job:
{% set allEntries = craft.entries
    .section(['publication','text', 'exhibition'])
    .order('GREATEST(releaseDate, publicationReleaseDate, exhibitionOpeningDate) desc')
    .limit(null) %}

> **Comments:**
> **username_2**: This totally should work, except that there’s a bug in Craft that is preventing it. Just fixed it for the next release. In the meantime, you will just need to add “field_” to the beginning of the first field handle in that `GREATEST()` list, e.g. `GREATEST(field_releaseDate, publicationReleaseDate, exhibitionOpeningDate)`. (The next update won’t break that, just make it not necessary.)  
> **username_0**: Thanks @Brandon, already noticed that bug and added "field_" to all field handles.  
> **username_0**: As I had already merged all those date fields into one before asking this question, I can't say yet if this code is indeed working. Will do some testing tomorrow.  
> **username_0**: By "merged" I meant that I copied the content from the separate fields into one single field, that I now use in all channels. Didn't come around to do the testing yet. Sorry for delaying the answer accept.  
> **username_0**: @Brandon I could as well stick to a single field in all channels, but then I'd like to give this field a slightly different name and different instructions in the individual channels. Currently not possible, is it?  
> **username_2**: @ChristianSeelbach Nope that’s not currently possible. This use case makes a good argument for it, though.  
> **username_3**: I realize this questions is almost a decade old, but is this still supported in Craft 4? When I try this...  `{% for entry in craft.entries.section(['articles','podcasts']).orderby('GREATEST(postDate, featuredDate) desc').all() %}` I get a SQL error.  

---

