I've got a WordPress database with a custom post type called events with custom fields for startdates and enddates of the events. I would like to output all of those events in a list having the upcoming events listed first in an ascending order and the rest listed in a descending order.

I tried to copy the code from this answer but i can't seem to get it right – all the posts are sorted ascendingly: [MySQL conditional ORDER BY ASC/DESC for date column](https://stackoverflow.com/questions/10626363/mysql-conditional-order-by-asc-desc-for-date-column)

This is my code right now:

```
SELECT p.post_title, m.meta_value 
FROM wp_posts as p, wp_postmeta as m 
WHERE p.ID = m.post_id 
    AND p.post_type = "events"
    AND m.meta_key = "startdate" 
    AND p.post_status = "publish"
ORDER BY m.meta_value ASC,
    CASE m.meta_value WHEN m.meta_value > DATE(NOW()) THEN m.meta_value END ASC,
    CASE WHEN m.meta_value < DATE(NOW()) THEN m.meta_value END DESC
LIMIT 10

```