Thanks to @cameronjonesweb’s link, here’s the code you might use:
add_filter( 'manage_edit-post_sortable_columns', 'my_add_sortable_custom_column');
function my_add_sortable_custom_column( $columns ) {
$columns['views'] = 'views';
return $columns;
}
add_action( 'pre_get_posts', 'my_sort_custom_column' );
function my_sort_custom_column( $query ) {
if( ! is_admin() || ! $query->is_main_query() ) {
return;
}
if ( 'views' === $query->get( 'orderby') ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', 'views' );
$query->set( 'meta_type', 'numeric' );
}
}
The first one registers the column as sortable, while the second changes the query when WordPress wants to sort by that column.