We are working on a project with a cpt and a date field for events.
My clients wants to order the events by date in the admin columns.
add_action( 'pre_get_posts', 'posttype_default_order' );
function posttype_default_order( $query ) {
if( ! is_admin() || ! $query->is_main_query() ) {
return;
}
if ( 'when' === $query->get( 'orderby') ) {
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', 'when' );
}
}
But this does not work. Because we want to order this way:
- 2020-01-01
- 2020-05-02
- 2020-06-11
- 2020-06-18
- 2019-05-02
- 2019-06-11
- 2018-03-08
- etc. etc. etc.
I’m not able to get this sorting correct.