The get_block_templates() WordPress PHP function retrieves a list of unified template objects based on a query.
Usage
get_block_templates($query = array(), $template_type = 'wp_template')
Parameters
- $query(array) – Optional. Arguments to retrieve templates. Default:- array().- slug__in– An array list of slugs to include.
- wp_id(int) – Post ID of customized template.
- area(string) – A- 'wp_template_part_area'taxonomy value to filter by (for- 'wp_template_part'template type only).
- post_type(string) – Post type to get the templates for.
 
- $template_type(string) – Optional.- 'wp_template'or- 'wp_template_part'. Default:- 'wp_template'.
More information
See WordPress Developer Resources: get_block_templates()
Examples
Get all templates of a specific post type
Retrieve all templates for the 'post' post type.
// Get all templates for 'post' post type
$templates = get_block_templates(array('post_type' => 'post'));
Get templates with specific slugs
Retrieve templates with slugs 'header' and 'footer'.
// Get templates with 'header' and 'footer' slugs
$templates = get_block_templates(array('slug__in' => array('header', 'footer')));
Get a single template by post ID
Retrieve a template with a specific post ID, such as 15.
// Get a template by post ID
$templates = get_block_templates(array('wp_id' => 15));
Get template parts with a specific area
Retrieve all 'wp_template_part' templates with a 'header' area.
// Get template parts with 'header' area
$template_parts = get_block_templates(array('area' => 'header'), 'wp_template_part');
Add default templates through a plugin
Set default templates by a plugin, ensuring they are overridden by templates from the theme and the editor.
add_filter('get_block_templates', function($query_result, $query, $template_type) {
    if (isset($query['theme']) && isset($query['slug__in']) && isset($query['slug__in'][1])) {
        foreach ($query['slug__in'] as $slug) {
            $template_file_path = plugin_dir_path(__FILE__) . '/templates/' . $slug . '.html';
            if (file_exists($template_file_path)) {
                $html = file_get_contents($template_file_path);
                array_push($query_result, (object) [
                    'title' => sprintf(__('Default %s', 'textdomain'), $slug),
                    'slug' => $slug,
                    'status' => 'publish',
                    'type' => 'wp_template',
                    'description' => sprintf(__('Default %s template', 'textdomain'), $slug),
                    'content' => $html,
                    'source' => 'plugin',
                    'is_custom' => false,
                    'is_customized' => false,
                    'is_reusable' => false,
                    'is_reserved' => false,
                    'is_published' => true,
                    'is_wp_template_part' => false,
                ]);
            }
        }
    }
    return $query_result;
}, 10, 3);