<p>The equivalent SQL would be something like</p>
<p><code>SELECT * FROM players ORDER BY teamName IS NOT NULL DESC, postDate DESC;</code></p>
<p>but I'm not sure how that would translate in a simple element query. It'd be something like:</p>
<p><code>{% set players = craft.entries.section('players').orderBy('teamName IS NOT NULL DESC, postDate desc').all() %}</code></p>

CC BY-SA 4.0
2021-04-20T09:14:59.090Z
2021-04-20T09:14:59.090Z
Wallace
<p>I believe this won't work the way you want it with a single query, especially with reliable performance. The problem is that you actually want 2 different sort criteria being combined.</p>
<p>I would recommend you add an additional field just for sorting using this plugin: <a href="https://plugins.craftcms.com/preparse-field" rel="nofollow noreferrer">https://plugins.craftcms.com/preparse-field</a></p>
<p>It allows you to use TWIG to prepare a value while saving the entry which can then be used for sorting. With that you can implement some logic to create a field value like this</p>
<pre><code>1-2021-03-03 (has a team)
1-2021-02-28 (has a team)
0-2021-03-01 (has no team)
0-2021-01-19 (has no team)
</code></pre>
<p>by using some TWIG code like this (untested):</p>
<pre><code>{{ (element.teamName.count() ? '1' : '0') ~ '-' ~ (element.postDate | date('Y-m-d')) }}
</code></pre>

CC BY-SA 4.0
2021-04-16T21:06:29.063Z
I made 2 queries of ids, merged those ids then made another query to get those entries in the order of the ids in the ids array, adding fixedOrder() to the query so it respected the order the id of the entry appeared in the ids array.
Brad
CC BY-SA 4.0
2021-04-18T16:32:48.393Z
I thought of this as well, but it won't scale as nice and needs multiple queries.
Matthias Redl-Mann
CC BY-SA 4.0
2021-04-16T19:24:42.687Z
2021-04-16T19:24:42.687Z
Matthias Redl-Mann
<p>Section handle: players
field handle: teamName</p>
<p>The checkbox field has two options:</p>
<ul>
<li>Cubs</li>
<li>Tigers</li>
</ul>
<p>Not all players will have the teamName field checked.</p>
<p>I want to order players who are Cubs or Tigers first and order those players by their postDate desc, while ordering the non Cubs/Tigers players by their postDate desc.</p>
<pre><code>{% set players = craft.entries.section('players').orderBy('teamName, postDate desc').all() %}
</code></pre>
<p>This kind of works, but it orders Cubs players before Tigers players, regardless of their postDate, so a Cubs player could of been added 30 days ago and still show up before a Tigers player who was added 2 days ago.</p>
<p>I know the teamName is being ordered, I don't know how to add the postDate desc to the teamName logic. It needs to support pagination.</p>

<entries><order><sort>
sort by checkbox field with multiple options and by postDate desc
# sort by checkbox field with multiple options and by postDate desc

(Asked by: username_0 on 2021-04-16)

Section handle: players
field handle: teamName
The checkbox field has two options:

Cubs
Tigers

Not all players will have the teamName field checked.
I want to order players who are Cubs or Tigers first and order those players by their postDate desc, while ordering the non Cubs/Tigers players by their postDate desc.
{% set players = craft.entries.section('players').orderBy('teamName, postDate desc').all() %}

This kind of works, but it orders Cubs players before Tigers players, regardless of their postDate, so a Cubs player could of been added 30 days ago and still show up before a Tigers player who was added 2 days ago.
I know the teamName is being ordered, I don't know how to add the postDate desc to the teamName logic. It needs to support pagination.

**Tags:** entries, order, sort


---

## 2 answers

### Answer by username_1 on 2021-04-20. Score: 2 | Accepted answer

The equivalent SQL would be something like

`SELECT * FROM players ORDER BY teamName IS NOT NULL DESC, postDate DESC;`

but I'm not sure how that would translate in a simple element query. It'd be something like:

`{% set players = craft.entries.section('players').orderBy('teamName IS NOT NULL DESC, postDate desc').all() %}`


---

### Answer by username_2 on 2021-04-16. Score: 1

I believe this won't work the way you want it with a single query, especially with reliable performance. The problem is that you actually want 2 different sort criteria being combined.
I would recommend you add an additional field just for sorting using this plugin: https://plugins.craftcms.com/preparse-field
It allows you to use TWIG to prepare a value while saving the entry which can then be used for sorting. With that you can implement some logic to create a field value like this
1-2021-03-03 (has a team)
1-2021-02-28 (has a team)
0-2021-03-01 (has no team)
0-2021-01-19 (has no team)

by using some TWIG code like this (untested):
{{ (element.teamName.count() ? '1' : '0') ~ '-' ~ (element.postDate | date('Y-m-d')) }}

> **Comments:**
> **username_0**: I made 2 queries of ids, merged those ids then made another query to get those entries in the order of the ids in the ids array, adding fixedOrder() to the query so it respected the order the id of the entry appeared in the ids array.  
> **username_2**: I thought of this as well, but it won't scale as nice and needs multiple queries.  

---

