I have a custom post type which uses dates and i have my custom columns set up in the admin. I need to be able to do order by that column by default - i have tried this code which I found on the net but after playing with it for an hour i still couldn't make it work and it only seemed to want to sort by title; can anyone help me.
1 Answer
I had to do this on a program management system. this should do the trick.
add_filter( 'pre_get_posts', 'my_sort_posts_listing' );
function my_sort_posts_listing( $wp_query ) {
if ( is_admin() && $wp_query->get( 'post_type' ) == 'my_post_type' ) {
$wp_query->set( 'meta_key', '_my_date_meta' );
$wp_query->set( 'orderby', 'meta_value' );
$wp_query->set( 'order', 'DESC' );
}
return $wp_query;
}
assuming your dates are stored like YYY-MM-DD