Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-12T18:26:13.093Z
Answers.Text: 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.)
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-12T20:55:59.280Z
Answers.Text: Thanks @Brandon, already noticed that bug and added "field_" to all field handles.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-12T21:01:44.603Z
Answers.Text: 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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-14T21:42:48.527Z
Answers.Text: 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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-14T21:43:11.653Z
Answers.Text: @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?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-14T21:46:10.740Z
Answers.Text: @ChristianSeelbach Nope that’s not currently possible. This use case makes a good argument for it, though.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-09T15:29:27.027Z
Answers.Text: 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.
Answers.UserDisplayName: BrianM
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-12T16:10:56.020Z
Answers.LastActivityDate: 2014-06-12T18:32:22.243Z
Answers.LastEditDate: 2014-06-12T18:32:22.243Z
Answers.UserDisplayName: Victor
Body: <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>
  
Tags: <elementcriteriamodel><join><sorting>
Title: How do you sort elements queried from multiple channels?
ThreadText: # 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](http://buildwithcraft.com/docs/templating/craft.entries#order) sets the `ORDER BY` part of the MySQL query. You can also pass MySQL functions in it.
  
  In your case [`GREATEST()`](http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_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 @username_2, 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**: @username_2 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.  
  
  ---
  
  