<p>To anyone else that needs to something similar:</p>
<pre><code>{% set query = {
  section: 'case_studies',
  limit: entry_limit,
  orderBy: 'featured desc, postDate desc',
}%}
</code></pre>
<p>The pagination is wonky without the secondary orderBy parameter. I'm guessing it's because the field for an inactive lightswitch is empty in the DB? I'm speculating, but prior to adding that it would just show the same projects over and over again.</p>
<p>I appreciate all who answered.</p>

CC BY-SA 4.0
2022-07-25T21:33:33.787Z
I'm implementing a very similar feature, but on a "structure" data set. I can't figure out what the second parameter would be instead of `postDate` to get the structure's ordering… Any idea? Thanks!
Yann
CC BY-SA 4.0
2020-08-10T16:02:22.467Z
2020-08-10T16:02:22.467Z
Jasen Peterson
<p>You can add an <code>orderBy</code> clause to the query, and order by the lightswitch field. For example, to fetch all the entries in section <code>things</code>, with the ones with the <code>featured</code> lightswitch turned on first, do this:</p>
<pre><code>  {% set things = craft.entries({
    section: 'things',
    orderBy: 'featured desc'
  }).all %}
</code></pre>

CC BY-SA 4.0
2020-08-08T22:05:08.800Z
2020-08-08T22:05:08.800Z
Marion Newlevant
<p>You're looking for the <code>filter</code> <a href="https://twig.symfony.com/doc/2.x/filters/filter.html" rel="nofollow noreferrer">filter</a>. Here's how you might do it:</p>
<pre><code>{% set entriesInCat = craft.entries.relatedTo(category).all() %}
{% set featuredEntriesInCat = entriesInCat|filter(entry =&gt; entry.yourLightswitchFieldHandle) %}
{% set remainder = entriesInCat|without(featuredEntriesInCat) %}

{% for item in featuredEntriesInCat %}
    &lt;h2&gt;{{ item.title }}&lt;/h2&gt;
{% endfor %}

&lt;hr&gt;

{% for item in remainder %}
    &lt;h2&gt;{{ item.title }}&lt;/h2&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2020-08-06T20:27:48.573Z
2020-08-06T20:27:48.573Z
James Smith
<p>I have a case studies filter that allows you to filter case studies by category. I've attached a light switch field called 'featured' to the entries that allow you to set it as featured. The idea is to have featured case studies appear first when filter results are returned. I've seen a few post discussing using an innerJoin to do this but I'm not sure if that's the right path or not. Any help would be greatly appreciated.</p>

<entries>
Order Entries Query by a Featured Light Switch
# Order Entries Query by a Featured Light Switch

(Asked by: username_0 on 2020-08-06)

I have a case studies filter that allows you to filter case studies by category. I've attached a light switch field called 'featured' to the entries that allow you to set it as featured. The idea is to have featured case studies appear first when filter results are returned. I've seen a few post discussing using an innerJoin to do this but I'm not sure if that's the right path or not. Any help would be greatly appreciated.

**Tags:** entries

---

## 3 answers

### Answer by username_0 on 2020-08-10. Score: 1 | Accepted answer

To anyone else that needs to something similar:
{% set query = {
  section: 'case_studies',
  limit: entry_limit,
  orderBy: 'featured desc, postDate desc',
}%}

The pagination is wonky without the secondary orderBy parameter. I'm guessing it's because the field for an inactive lightswitch is empty in the DB? I'm speculating, but prior to adding that it would just show the same projects over and over again.
I appreciate all who answered.

> **Comments:**
> **username_1**: I'm implementing a very similar feature, but on a "structure" data set. I can't figure out what the second parameter would be instead of `postDate` to get the structure's ordering… Any idea? Thanks!  

---

### Answer by username_2 on 2020-08-08. Score: 1

You can add an orderBy clause to the query, and order by the lightswitch field. For example, to fetch all the entries in section things, with the ones with the featured lightswitch turned on first, do this:
  {% set things = craft.entries({
    section: 'things',
    orderBy: 'featured desc'
  }).all %}


---

### Answer by username_3 on 2020-08-06. Score: 1

You're looking for the filter filter. Here's how you might do it:
{% set entriesInCat = craft.entries.relatedTo(category).all() %}
{% set featuredEntriesInCat = entriesInCat|filter(entry => entry.yourLightswitchFieldHandle) %}
{% set remainder = entriesInCat|without(featuredEntriesInCat) %}

{% for item in featuredEntriesInCat %}
    {{ item.title }}
{% endfor %}



{% for item in remainder %}
    {{ item.title }}
{% endfor %}


---

