<p>To accomplish what you're after you'd have to write some lines of PHP. Listen to the onSaveEntry event from a custom plugin and re-save all the other entries with the lightswitch field disabled if the one you just edited got it enabled.</p>

<p>You could also think about refactoring and using an Entries field somewhere to select the featured entry, i.e. in the homepage's Single section or a Global Set.</p>

CC BY-SA 3.0
2016-03-29T00:11:47.870Z
That seems to be what [this question](http://craftcms.stackexchange.com/questions/14352/programatically-change-a-lightswitch-value-on-an-entry-on-save) is trying to get at.
jasonetco
CC BY-SA 3.0
2016-03-29T07:20:57.347Z
Yeah that's the code to the first approach I had in mind. Never implemented it though, as I actually really like to just use an Entries field.
carlcs
CC BY-SA 3.0
2016-03-28T15:33:49.940Z
2016-03-28T15:33:49.940Z
carlcs
<p>It depends on your sort order. If your goal is to only display the most recent 'featured' entry, then you could add the <strong>.first</strong> parameter to your template code. </p>

<p>Craft would simply grab the first entry it finds</p>

<p>OR, you could limit your query by using the Limit parameter. See: <a href="https://craftcms.com/docs/templating/craft.entries#limit" rel="nofollow noreferrer">https://craftcms.com/docs/templating/craft.entries#limit</a></p>

<hr>

<p>Edit (addition):
Here's an example of how you could display the single, most recent entry which has been marked as <strong>Featured</strong>, using a lightswitch field.</p>

<pre><code>{% set featuredNews = craft.entries.section('news').featured('not 0').order('dateUpdated desc').limit(1) %}
{% for entry in featuredNews %}
    {{ entry.title }}
{% endfor %}
</code></pre>

<p>(Some valuable insight here: <a href="https://craftcms.stackexchange.com/questions/11911/craft-entries-and-lightswitch">craft.entries and lightswitch</a> and here: <a href="https://craftcms.com/docs/lightswitch-fields" rel="nofollow noreferrer">https://craftcms.com/docs/lightswitch-fields</a>)</p>

<p>Hope it helps</p>

CC BY-SA 3.0
2016-03-29T00:14:15.090Z
Right, the `.first` parameter is great for that, but the "Featured" entry might need to be different from the most recent entry.
jasonetco
CC BY-SA 3.0
2016-03-29T05:45:13.313Z
@jasonetco I understand. Is the single featured entry, the most recent entry which has been marked as featured? (How do you determine which of the featured entries gets displayed?)
Matt P
CC BY-SA 3.0
2016-03-29T14:56:40.297Z
ideally it would be a manual choice, so not an automated "Whichever entry is most recent," but a decision to make this 3 year old entry featured and no other entry.
jasonetco
CC BY-SA 3.0
2016-03-29T15:13:23.463Z
I've added `.order('dateUpdated desc')` as a parameter above, this would allow you to simply re-save the Featured entry, leaving the Date Created unchanged. What do you think?
Matt P
CC BY-SA 3.0
2016-03-29T16:11:10.353Z
I don't think that's a good idea: you'll eventually end up with lots of entries marked as featured. The OP wants do manually select a single Featured Entry, independent of the dateUpdated or postDate date.
carlcs
CC BY-SA 3.0
2016-03-29T16:18:26.080Z
I hear you, fair point, Carl. Personally I think that if a lot of entries have been marked as 'Featured' then they may be useful elsewhere on the site (where more than 1 'Featured' entry needs to be displayed).

On the other hand, if the OP only every wants 1 entry to be featured on a specific template, then your answer below (using an Entries field on a Single e.g. home page) would be my choice. 

At the end of the day, it needs to be simple for the CMS admin.
Matt P
CC BY-SA 3.0
2016-03-28T15:15:39.023Z
2016-03-29T15:11:07.447Z
2017-04-13T12:50:59.930Z
Matt P
<p>Assuming I'm using a lightswitch to denote an entry as "Featured," how can I then disable that lightswitch on every other entry, so that no matter how many times I flip these switches only one entry can be "Featured?"</p>

<p>Thanks!</p>

<p><br/></p>

<p><em>Edit: There are some <a href="https://craftcms.stackexchange.com/questions/14352/programatically-change-a-lightswitch-value-on-an-entry-on-save">plugin options</a>, and that might be the only way to go but I'm hoping that someone can find a better way through Twig filters or something.</em></p>

CC BY-SA 3.0
2016-03-29T00:15:55.887Z
The `Limit` parameter, used to filter entries with the switch turned on, then used in conjunction with the `.first` parameter might work. It would only take the most recent article that has the switch enabled; it's nowhere near a perfect solution though.
jasonetco
CC BY-SA 3.0
2016-03-29T16:12:15.047Z
It's not gonna work through Twig filters. A custom plugin making use of the entry events is a good solution for this problem. Don't fear writting that little plugin, it's really not that hard.
carlcs
<templating><control-panel><lightswitch>
When I flip the "Featured Entry" switch, disable it on other entries
# When I flip the "Featured Entry" switch, disable it on other entries

(Asked by: username_0 on 2016-03-28)

Assuming I'm using a lightswitch to denote an entry as "Featured," how can I then disable that lightswitch on every other entry, so that no matter how many times I flip these switches only one entry can be "Featured?"

Thanks!

*Edit: There are some [plugin options](https://craftcms.stackexchange.com/questions/14352/programatically-change-a-lightswitch-value-on-an-entry-on-save), and that might be the only way to go but I'm hoping that someone can find a better way through Twig filters or something.*

**Tags:** templating, control-panel, lightswitch
> **Comments:**
> **username_0**: The `Limit` parameter, used to filter entries with the switch turned on, then used in conjunction with the `.first` parameter might work. It would only take the most recent article that has the switch enabled; it's nowhere near a perfect solution though.  
> **username_1**: It's not gonna work through Twig filters. A custom plugin making use of the entry events is a good solution for this problem. Don't fear writting that little plugin, it's really not that hard.  

---

## 2 answers

### Answer by username_1 on 2016-03-28. Score: 3 | Accepted answer

To accomplish what you're after you'd have to write some lines of PHP. Listen to the onSaveEntry event from a custom plugin and re-save all the other entries with the lightswitch field disabled if the one you just edited got it enabled.

You could also think about refactoring and using an Entries field somewhere to select the featured entry, i.e. in the homepage's Single section or a Global Set.

> **Comments:**
> **username_0**: That seems to be what [this question](http://craftcms.stackexchange.com/questions/14352/programatically-change-a-lightswitch-value-on-an-entry-on-save) is trying to get at.  
> **username_1**: Yeah that's the code to the first approach I had in mind. Never implemented it though, as I actually really like to just use an Entries field.  

---

### Answer by username_2 on 2016-03-28. Score: 3

It depends on your sort order. If your goal is to only display the most recent 'featured' entry, then you could add the .first parameter to your template code. 
Craft would simply grab the first entry it finds
OR, you could limit your query by using the Limit parameter. See: https://craftcms.com/docs/templating/craft.entries#limit

Edit (addition):
Here's an example of how you could display the single, most recent entry which has been marked as Featured, using a lightswitch field.
{% set featuredNews = craft.entries.section('news').featured('not 0').order('dateUpdated desc').limit(1) %}
{% for entry in featuredNews %}
    {{ entry.title }}
{% endfor %}

(Some valuable insight here: craft.entries and lightswitch and here: https://craftcms.com/docs/lightswitch-fields)
Hope it helps

> **Comments:**
> **username_0**: Right, the `.first` parameter is great for that, but the "Featured" entry might need to be different from the most recent entry.  
> **username_2**: @jasonetco I understand. Is the single featured entry, the most recent entry which has been marked as featured? (How do you determine which of the featured entries gets displayed?)  
> **username_0**: ideally it would be a manual choice, so not an automated "Whichever entry is most recent," but a decision to make this 3 year old entry featured and no other entry.  
> **username_2**: I've added `.order('dateUpdated desc')` as a parameter above, this would allow you to simply re-save the Featured entry, leaving the Date Created unchanged. What do you think?  
> **username_1**: I don't think that's a good idea: you'll eventually end up with lots of entries marked as featured. The OP wants do manually select a single Featured Entry, independent of the dateUpdated or postDate date.  
> **username_2**: I hear you, fair point, Carl. Personally I think that if a lot of entries have been marked as 'Featured' then they may be useful elsewhere on the site (where more than 1 'Featured' entry needs to be displayed).  On the other hand, if the OP only every wants 1 entry to be featured on a specific template, then your answer below (using an Entries field on a Single e.g. home page) would be my choice.   At the end of the day, it needs to be simple for the CMS admin.  

---

