resolve_block_template( string $template_type, string[] $template_hierarchy )

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Return the correct ‘wp_template’ to render for the request template type.

Parameters

$template_type

(string) (Required) The current template type.

$template_hierarchy

(string[]) (Required) The current template hierarchy, ordered by priority.

Return

(WP_Block_Template|null) template A template object, or null if none could be found.

Source

File: wp-includes/block-template.php

function resolve_block_template( $template_type, $template_hierarchy ) {
	if ( ! $template_type ) {
		return null;
	}

	if ( empty( $template_hierarchy ) ) {
		$template_hierarchy = array( $template_type );
	}

	$slugs = array_map(
		'_strip_template_file_suffix',
		$template_hierarchy
	);

	// Find all potential templates 'wp_template' post matching the hierarchy.
	$query     = array(
		'theme'    => wp_get_theme()->get_stylesheet(),
		'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,
		function ( $template_a, $template_b ) use ( $slug_priorities ) {
			return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
		}
	);

	return count( $templates ) ? $templates[0] : null;
}

Changelog

Version Description
5.8.0 Introduced.

© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/resolve_block_template