Answers.Body: <p>There are several ways you could attack this, but probably the easiest is directly from the SQL side with something like this using a SQL CASE statement:</p>
  
  <pre><code>{% set entries = craft.entries.section('news').order('(CASE WHEN field_alphaTitle IS NULL OR field_alphaTitle = "" THEN title ELSE field_AlphaTitle END)').find() %}
  
  &lt;ul&gt;
      {% for entry in entries %}
          &lt;li&gt;TITLE = {{ entry.title }} / ALPHA TITLE = {{ entry.alphaTitle }}&lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
  </code></pre>
  
  <p>Note that is assuming that <code>alphaTitle</code> is a custom field and <code>title</code> is the standard one available to all entries.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-09T23:18:39.753Z
Answers.Text: @BeckySoll Please mark this answer as correct if it solved your problem. This gives credit to Brad.
Answers.UserDisplayName: Anna_MediaGirl
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-25T19:24:55.620Z
Answers.Text: beautiful, that worked!
Answers.UserDisplayName: Becky Soll
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-25T19:34:30.700Z
Answers.Text: Glad it did! Would you mind marking it as the answer?
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-25T18:00:35.740Z
Answers.LastActivityDate: 2014-08-25T18:00:35.740Z
Answers.UserDisplayName: Brad Bell
Body: <p>Is it possible to put conditions on the ordering of a loop of entries? </p>
  
  <p>I have two fields: title and alphaTitle. I want to order alphabetically, by alphaTitle if it has a value, otherwise by title. Title will always have a value but I only want to order by title if there is no alphaTitle. I've been having some trouble figuring out if this is possible or how to do it - any chance someone can point me in the right direction?</p>
  
Tags: <entries><conditional><ordering>
Title: From a template, how can you order entries by one field, and if that field is empty a different field?
ThreadText: # From a template, how can you order entries by one field, and if that field is empty a different field?
  
  (Asked by: username_0 on 2014-08-25)
  
  Is it possible to put conditions on the ordering of a loop of entries?
  
  I have two fields: title and alphaTitle. I want to order alphabetically, by alphaTitle if it has a value, otherwise by title. Title will always have a value but I only want to order by title if there is no alphaTitle. I've been having some trouble figuring out if this is possible or how to do it - any chance someone can point me in the right direction?
  
  **Tags:** entries, conditional, ordering
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-08-25. Score: 12
  
  There are several ways you could attack this, but probably the easiest is directly from the SQL side with something like this using a SQL CASE statement:
  
  ```
  {% set entries = craft.entries.section('news').order('(CASE WHEN field_alphaTitle IS NULL OR field_alphaTitle = "" THEN title ELSE field_AlphaTitle END)').find() %}
  
  <ul>
      {% for entry in entries %}
          <li>TITLE = {{ entry.title }} / ALPHA TITLE = {{ entry.alphaTitle }}</li>
      {% endfor %}
  </ul>
  
  ```
  
  Note that is assuming that `alphaTitle` is a custom field and `title` is the standard one available to all entries.
  
  > **Comments:**
  > **username_0**: beautiful, that worked!  
  > **username_1**: Glad it did! Would you mind marking it as the answer?  
  > **username_2**: @username_0 Please mark this answer as correct if it solved your problem. This gives credit to username_1.  
  
  ---
  
  