<p>If you want to order by multiple columns that include custom fields, first make sure you're at least on <a href="http://buildwithcraft.com/updates#build2684" rel="noreferrer">Craft 2.4.2684</a>, then you'll be able to use this syntax:</p>

<pre><code>{% for gallery in craft.entries.section('Gallery').order('postDate desc, available desc').limit(null) %}
</code></pre>

<p>Also note that that you should be using <code>limit(null)</code> instead of <code>limit(none)</code> if you want to result all results.</p>

CC BY-SA 3.0
2015-03-19T18:12:25.107Z
For some reason the syntax you showed gives me an error `CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'available' in 'order clause'`, but wen reversing the order it worked as expected :) `.order('available desc, postDate desc')`
Ricardo Henriquez
CC BY-SA 3.0
2015-03-19T18:13:26.080Z
What's the error?
Brad Bell
CC BY-SA 3.0
2015-03-19T18:20:04.547Z
`CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'available' in 'order clause'`
Ricardo Henriquez
CC BY-SA 3.0
2015-03-19T22:25:31.777Z
You have a custom field with an `available` handle in the field layout of your `Gallery` section?
Brad Bell
CC BY-SA 3.0
2015-03-20T23:19:13.043Z
Yes, I do have a custom field with a handle available
Ricardo Henriquez
CC BY-SA 3.0
2015-03-19T17:21:15.680Z
2015-09-01T08:56:38.297Z
2015-09-01T08:56:38.297Z
Brad Bell
<p>Currently I am sorting the entries by an ID:</p>

<pre><code>{% for gallery in craft.entries.section('Gallery').order('available desc').limit(none) %}
</code></pre>

<p>but I would like to show the latest entries on top, currently it shows available first but latest entries ones go to the bottom. How can I mix the above code with something like this?</p>

<pre><code>.order('postDate desc') %}
</code></pre>

CC BY-SA 3.0
2015-03-19T16:30:41.207Z
At the risk of asking a stupid question, have you tried just chaining them? `.order('this asc').order('that desc').limit(10)`. Just a hunch, but I can't test it out right now.
Stephen Lewis
CC BY-SA 3.0
2015-03-19T18:04:43.813Z
I did... it only recognizes the last .order :(
Ricardo Henriquez
<templating><entries><sorting>
How do I sort entries by two variables?
# How do I sort entries by two variables?

(Asked by: username_0 on 2015-03-19)

Currently I am sorting the entries by an ID:
{% for gallery in craft.entries.section('Gallery').order('available desc').limit(none) %}

but I would like to show the latest entries on top, currently it shows available first but latest entries ones go to the bottom. How can I mix the above code with something like this?
.order('postDate desc') %}

**Tags:** templating, entries, sorting

> **Comments:**
> **username_1**: At the risk of asking a stupid question, have you tried just chaining them? `.order('this asc').order('that desc').limit(10)`. Just a hunch, but I can't test it out right now.  
> **username_0**: I did... it only recognizes the last .order :(  

---

## 1 answers

### Answer by username_2 on 2015-03-19. Score: 18 | Accepted answer

If you want to order by multiple columns that include custom fields, first make sure you're at least on Craft 2.4.2684, then you'll be able to use this syntax:
{% for gallery in craft.entries.section('Gallery').order('postDate desc, available desc').limit(null) %}

Also note that that you should be using limit(null) instead of limit(none) if you want to result all results.

> **Comments:**
> **username_0**: For some reason the syntax you showed gives me an error `CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'available' in 'order clause'`, but wen reversing the order it worked as expected :) `.order('available desc, postDate desc')`  
> **username_2**: What's the error?  
> **username_0**: `CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'available' in 'order clause'`  
> **username_2**: You have a custom field with an `available` handle in the field layout of your `Gallery` section?  
> **username_0**: Yes, I do have a custom field with a handle available  

---

