<p>I don't know if it's possible (but would like to see a query solution).</p>

<p>There is one problem with your setup: If an entry gets a "substantial update", you can't make a correction afterwards, since it would become the latest item again.</p>

<p>I propose you write a plugin which would:</p>

<ul>
<li>Wait for the <a href="http://buildwithcraft.com/docs/plugins/events-reference#entries-onBeforeSaveEntry" rel="nofollow">'onBeforeSaveEntry' event</a></li>
<li>Check if the <a href="http://buildwithcraft.com/docs/lightswitch-fields" rel="nofollow">Lightswitch-field</a> is "on"</li>
<li>If true, set <a href="http://buildwithcraft.com/docs/templating/entrymodel#postDate" rel="nofollow">postDate</a> to the current date</li>
<li>Set the Lightswitch-field to "off" again</li>
</ul>

<p>.</p>

<p><strong>Quick-and-Dirty example:</strong></p>

<pre class="lang-php prettyprint-override"><code>public function init()
{
    parent::init();

    //Event: onBeforeSaveEntry
    craft()-&gt;entries-&gt;onBeforeSaveEntry = function(Event $event) {

        //Check if its the right section
        if ( $event-&gt;entry-&gt;section == 'news' ) {

            //Check if the lightswitch-field is "on"
            if ( $event-&gt;entry-&gt;lightswitch_field_handle == 1 ) {
                //Set postDate to current date
                $event-&gt;entry-&gt;postDate = $event-&gt;entry-&gt;dateUpdated;

                //Set Lightswitch-filed to "off"
                $event-&gt;entry-&gt;lightswitch_field_handle = 0;
            }

        }

        //Return
        return $event;

    };
}
</code></pre>

<p>If you add this function to your <a href="http://buildwithcraft.com/docs/plugins/setting-things-up#your-primary-plugin-class" rel="nofollow">primary plugin class</a> and sort your entries by <code>postDate</code> it should work just like you want.</p>

CC BY-SA 3.0
2014-07-08T13:33:01.623Z
This is quite an interesting solution, wondering if we could produce a solution based around this idea. Thank you :)
Ian Nebbiolo
CC BY-SA 3.0
2014-07-08T13:52:16.013Z
Forgot to say that you just need to add this function to your primary plugin class and sort your entries by postDate.
Victor
CC BY-SA 3.0
2014-07-08T14:27:54.927Z
If you're going to the trouble of resetting the postDate programatically, why don't you just ask your client to change the postDate themselves—they'd be changing the lightswitch field anyway? This would give you the exact same result but without any extra code.
Stuart Whitehead
CC BY-SA 3.0
2014-07-08T14:29:53.467Z
@StuartWhitehead I really don't know why I haven't thought of that -.-
Victor
CC BY-SA 3.0
2014-07-09T16:56:35.567Z
@VictorIn Your staggering SE rep is clouding your judgment, Victor. :)
Matt Stein
CC BY-SA 3.0
2014-07-10T06:54:39.183Z
@StuartWhitehead VictorIn Thanks guys. We did consider that as a solution too ;)
Ian Nebbiolo
CC BY-SA 3.0
2014-07-08T12:31:19.293Z
2014-07-08T13:51:13.797Z
2014-07-08T13:51:13.797Z
Victor
<p>My <a href="http://michaelrog.com/craft/lagniappe/supersort" rel="nofollow">SuperSort</a> plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.</p>

<p>So, you could do something like:</p>

<pre><code>{% set sortedEntries = entries | supersort('sortAs', '{{ object.myLightswitch == 'y' ? object.dateUpdated: object.postDate }}') %}
</code></pre>

CC BY-SA 3.0
2015-01-24T00:32:34.453Z
2015-01-24T00:32:34.453Z
Michael Rog
<p>I think I'd create a <a href="http://twig.sensiolabs.org/doc/advanced.html#functions" rel="nofollow">twig function</a> or filter to create a custom sort (<a href="http://www.php.net/manual/en/function.usort.php" rel="nofollow">usort</a> in php), pass the collection to the function so it's returned 'sorted'.</p>

<p>Would be interested in seeing if anyone has a nice craft.entries query straight off the bat for this though.</p>

CC BY-SA 3.0
2014-07-08T12:05:31.110Z
2014-07-08T12:30:17.740Z
2014-07-08T12:30:17.740Z
billythekid
<p>You can do this entirely from the <a href="http://buildwithcraft.com/docs/templating/craft.entries#order"><code>order</code></a> param using MySQL’s <a href="http://dev.mysql.com/doc/refman/5.0/en/case.html"><code>CASE</code></a> syntax:</p>

<pre><code>{% set newsItems = craft.entries({
    section: 'news',
    order:   '(CASE field_myLightswitchField
                  WHEN 1 THEN elements.dateUpdated
                  ELSE elements.dateCreated
              END) DESC'
}) %}
</code></pre>

<p>Note that you’ll need to include that “field_” prefix before your Lightswitch field’s handle since this is a more complex condition and Craft won’t be sure if it should add that automatically.</p>

CC BY-SA 3.0
2014-07-08T14:45:21.063Z
2014-07-08T14:45:21.063Z
Brandon Kelly
<p>I'm trying to work out how I can order news posts by postDate, unless a lightswitch is toggled for a post, at which point it will use the dateUpdated to order that item.</p>

<p>This is so a client can still change posts without them appearing at the top of the list (for corrections, etc), but also make substantial updates to certain posts and have them appear as the latest newest item.</p>

<entries><ordering><postdate>
No ordinary news post ordering
# No ordinary news post ordering

(Asked by: username_0 on 2014-07-08)

I'm trying to work out how I can order news posts by postDate, unless a lightswitch is toggled for a post, at which point it will use the dateUpdated to order that item.

This is so a client can still change posts without them appearing at the top of the list (for corrections, etc), but also make substantial updates to certain posts and have them appear as the latest newest item.

**Tags:** entries, ordering, postdate

---

## 4 answers

### Answer by username_6 on 2014-07-08. Score: 8 | Accepted answer

You can do this entirely from the order param using MySQL’s CASE syntax:
{% set newsItems = craft.entries({
    section: 'news',
    order:   '(CASE field_myLightswitchField
                  WHEN 1 THEN elements.dateUpdated
                  ELSE elements.dateCreated
              END) DESC'
}) %}

Note that you’ll need to include that “field_” prefix before your Lightswitch field’s handle since this is a more complex condition and Craft won’t be sure if it should add that automatically.


---

### Answer by username_1 on 2014-07-08. Score: 4

I don't know if it's possible (but would like to see a query solution).
There is one problem with your setup: If an entry gets a "substantial update", you can't make a correction afterwards, since it would become the latest item again.
I propose you write a plugin which would:

Wait for the 'onBeforeSaveEntry' event
Check if the Lightswitch-field is "on"
If true, set postDate to the current date
Set the Lightswitch-field to "off" again

.
Quick-and-Dirty example:
public function init()
{
    parent::init();

    //Event: onBeforeSaveEntry
    craft()->entries->onBeforeSaveEntry = function(Event $event) {

        //Check if its the right section
        if ( $event->entry->section == 'news' ) {

            //Check if the lightswitch-field is "on"
            if ( $event->entry->lightswitch_field_handle == 1 ) {
                //Set postDate to current date
                $event->entry->postDate = $event->entry->dateUpdated;

                //Set Lightswitch-filed to "off"
                $event->entry->lightswitch_field_handle = 0;
            }

        }

        //Return
        return $event;

    };
}

If you add this function to your primary plugin class and sort your entries by postDate it should work just like you want.

> **Comments:**
> **username_0**: This is quite an interesting solution, wondering if we could produce a solution based around this idea. Thank you :)  
> **username_1**: Forgot to say that you just need to add this function to your primary plugin class and sort your entries by postDate.  
> **username_2**: If you're going to the trouble of resetting the postDate programatically, why don't you just ask your client to change the postDate themselves—they'd be changing the lightswitch field anyway? This would give you the exact same result but without any extra code.  
> **username_1**: @StuartWhitehead I really don't know why I haven't thought of that -.-  
> **username_3**: @VictorIn Your staggering SE rep is clouding your judgment, Victor. :)  
> **username_0**: @StuartWhitehead VictorIn Thanks guys. We did consider that as a solution too ;)  

---

### Answer by username_4 on 2015-01-24. Score: 3

My SuperSort plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.
So, you could do something like:
{% set sortedEntries = entries | supersort('sortAs', '{{ object.myLightswitch == 'y' ? object.dateUpdated: object.postDate }}') %}


---

### Answer by username_5 on 2014-07-08. Score: 3

I think I'd create a [twig function](http://twig.sensiolabs.org/doc/advanced.html#functions) or filter to create a custom sort ([usort](http://www.php.net/manual/en/function.usort.php) in php), pass the collection to the function so it's returned 'sorted'.

Would be interested in seeing if anyone has a nice craft.entries query straight off the bat for this though.


---

