Managing event dates vs published dates in admin custom post type

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

Leave a Comment