Answer by @Gareth Gillman definitely works but its an expensive query if we have hundreds of posts.
The function below is less expensive and using the wp native function get_post_custom_keys()
if ( ! function_exists( 'us_get_meta_keys_for_post_type' ) ) :
function us_get_meta_keys_for_post_type( $post_type, $sample_size = 5 ) {
$meta_keys = array();
$posts = get_posts( array( 'post_type' => $post_type, 'limit' => $sample_size ) );
foreach ( $posts as $post ) {
$post_meta_keys = get_post_custom_keys( $post->ID );
$meta_keys = array_merge( $meta_keys, $post_meta_keys );
}
// Use array_unique to remove duplicate meta_keys that we received from all posts
// Use array_values to reset the index of the array
return array_values( array_unique( $meta_keys ) );
}
endif;
You may use the $sample_size param to include more posts in the loop.
Hope it helps someone.