WordPress Admin - Automatically Sort Custom Posts by Custom Field Date Value in d-m-Y Fomat

by Connor   Last Updated July 12, 2019 09:08 AM - source

I have a custom post type called 'Courses'. I'm currently trying to automatically display them in the dashboard 'Courses' post list sorted by a date value specified in a custom field.

The date value is saved in d-m-Y format.

Using the code below I am able to sort the posts, but they are incorrectly ordered by the first number in the full date string (ie. the '01' in '01-12-19'), rather than the date.

function display_custom_posts_sorted_by_date($query) {
    if (is_admin()) {
        if (isset($query->query_vars['post_type'])) {
            if ($query->query_vars['post_type'] == 'courses') {
                $query->set('meta_key', 'online_start');
                $query->set('orderby', 'meta_value');
                $query->set('order', 'DESC');
            }
        }
    }
}
add_filter('pre_get_posts' , 'display_custom_posts_sorted_by_date');

I already have functionality in place to manually sort the post list using a filterable column, discussed here - How to Sort by Date When Using d-m-Y Format

Is it possible to amend the 'meta_key' query in the function above so that it recognizes the d-m-Y date format?



Related Questions


Order by custom field date with ASC order

Updated February 05, 2017 14:03 PM

How do I display posts ordered by a date custom field?

Updated September 29, 2017 01:08 AM