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?"


Edit: There are some plugin options, 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.

  • 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 Mar 29 '16 at 0:15
  • 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 Mar 29 '16 at 16:12

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.

| improve this answer | |
  • That seems to be what this question is trying to get at. – jasonetco Mar 29 '16 at 0:11
  • 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 Mar 29 '16 at 7:20

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

| improve this answer | |
  • 1
    Right, the .first parameter is great for that, but the "Featured" entry might need to be different from the most recent entry. – jasonetco Mar 29 '16 at 0:14
  • @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 Mar 29 '16 at 5:45
  • 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 Mar 29 '16 at 14:56
  • 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 Mar 29 '16 at 15:13
  • 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. – carlcs Mar 29 '16 at 16:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.