Having a hard time sorting a project list using startdate. This is what I use
// Sort Projects by end-date
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'project' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'project' ) {
$query->set('orderby', 'meta_value_num');
$query->set('meta_type', 'DATE');
$query->set('meta_key', 'startdatum');
$query->set('order', 'ASC');
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
but this does not affect sort order. What am I overlooking?
Hi @nowton
Have you made sure that you even make it inside the if statement? Perhaps there’s something with it that you’ve overlooked so the conditions aren’t met.
Also, I don’t think you need to set orderby to meta_value_num for dates.. I think meta_value is quite enough especially if you have a format like say Y-m-d
Sorry for not getting back to you sooner: not sure what happened there, but apparently the forementioned code is now functional. Thanks.