$slugs =
array_map( '_strip_template_file_suffix',
$template_hierarchy );
// Find all potential templates 'wp_template' post matching the hierarchy.
$query = array
( 'slug__in' =>
$slugs,
);
$templates =
get_block_templates( $query );
// Order these templates per slug priority.
// Build map of template slugs to their priority in the current hierarchy.
$slug_priorities =
array_flip( $slugs );
usort( $templates,
static function D
$template_a,
$template_b ) use ( $slug_priorities ) { return $slug_priorities[ $template_a->slug
] -
$slug_priorities[ $template_b->slug
];
} );