Title: _get_block_templates_files
Published: February 3, 2022
Last modified: May 20, 2026

---

# _get_block_templates_files( string $template_type, array $query = array() ): array|null

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#wp--skip-link--target)

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

Retrieves the template files from the theme.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#parameters)󠁿

 `$template_type`stringrequired

Template type. Either `'wp_template'` or `'wp_template_part'`.

`$query`arrayoptional

Arguments to retrieve templates. Optional, empty by default.

 * `slug__in` string[]
 * List of slugs to include.
 * `slug__not_in` string[]
 * List of slugs to skip.
 * `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.

Default:`array()`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#return)󠁿

 array|null Template files on success, null if `$template_type` is not matched.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#source)󠁿

    ```php
    function _get_block_templates_files( $template_type, $query = array() ) {
    	if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
    		return null;
    	}

    	$default_template_types = array();
    	if ( 'wp_template' === $template_type ) {
    		$default_template_types = get_default_block_template_types();
    	}

    	// Prepare metadata from $query.
    	$slugs_to_include = $query['slug__in'] ?? array();
    	$slugs_to_skip    = $query['slug__not_in'] ?? array();
    	$area             = $query['area'] ?? null;
    	$post_type        = $query['post_type'] ?? '';

    	$stylesheet = get_stylesheet();
    	$template   = get_template();
    	$themes     = array(
    		$stylesheet => get_stylesheet_directory(),
    	);
    	// Add the parent theme if it's not the same as the current theme.
    	if ( $stylesheet !== $template ) {
    		$themes[ $template ] = get_template_directory();
    	}
    	$template_files = array();
    	foreach ( $themes as $theme_slug => $theme_dir ) {
    		$template_base_paths  = get_block_theme_folders( $theme_slug );
    		$theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] );
    		foreach ( $theme_template_files as $template_file ) {
    			$template_base_path = $template_base_paths[ $template_type ];
    			$template_slug      = substr(
    				$template_file,
    				// Starting position of slug.
    				strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ),
    				// Subtract ending '.html'.
    				-5
    			);

    			// Skip this item if its slug doesn't match any of the slugs to include.
    			if ( ! empty( $slugs_to_include ) && ! in_array( $template_slug, $slugs_to_include, true ) ) {
    				continue;
    			}

    			// Skip this item if its slug matches any of the slugs to skip.
    			if ( ! empty( $slugs_to_skip ) && in_array( $template_slug, $slugs_to_skip, true ) ) {
    				continue;
    			}

    			/*
    			 * The child theme items (stylesheet) are processed before the parent theme's (template).
    			 * If a child theme defines a template, prevent the parent template from being added to the list as well.
    			 */
    			if ( isset( $template_files[ $template_slug ] ) ) {
    				continue;
    			}

    			$new_template_item = array(
    				'slug'  => $template_slug,
    				'path'  => $template_file,
    				'theme' => $theme_slug,
    				'type'  => $template_type,
    			);

    			if ( 'wp_template_part' === $template_type ) {
    				$candidate = _add_block_template_part_area_info( $new_template_item );
    				if ( ! isset( $area ) || $area === $candidate['area'] ) {
    					$template_files[ $template_slug ] = $candidate;
    				}
    			}

    			if ( 'wp_template' === $template_type ) {
    				$candidate = _add_block_template_info( $new_template_item );
    				$is_custom = ! isset( $default_template_types[ $candidate['slug'] ] );

    				if (
    					! $post_type ||
    					( $post_type && isset( $candidate['postTypes'] ) && in_array( $post_type, $candidate['postTypes'], true ) )
    				) {
    					$template_files[ $template_slug ] = $candidate;
    				}

    				// The custom templates with no associated post types are available for all post types.
    				if ( $post_type && ! isset( $candidate['postTypes'] ) && $is_custom ) {
    					$template_files[ $template_slug ] = $candidate;
    				}
    			}
    		}
    	}

    	return array_values( $template_files );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/block-template-utils.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/block-template-utils.php#L386)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/block-template-utils.php#L386-L477)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#related)󠁿

| Uses | Description | 
| [get_default_block_template_types()](https://developer.wordpress.org/reference/functions/get_default_block_template_types/)`wp-includes/block-template-utils.php` |

Returns a filtered list of default template types, containing their localized titles and descriptions.

  | 
| [_get_block_templates_paths()](https://developer.wordpress.org/reference/functions/_get_block_templates_paths/)`wp-includes/block-template-utils.php` |

Finds all nested template part file paths in a theme’s directory.

  | 
| [_add_block_template_part_area_info()](https://developer.wordpress.org/reference/functions/_add_block_template_part_area_info/)`wp-includes/block-template-utils.php` |

Attempts to add the template part’s area information to the input template.

  | 
| [_add_block_template_info()](https://developer.wordpress.org/reference/functions/_add_block_template_info/)`wp-includes/block-template-utils.php` |

Attempts to add custom template information to the template item.

  | 
| [get_block_theme_folders()](https://developer.wordpress.org/reference/functions/get_block_theme_folders/)`wp-includes/block-template-utils.php` |

For backward compatibility reasons, block themes might be using block-templates or block-template-parts, this function ensures we fallback to these folders properly.

  | 
| [get_template()](https://developer.wordpress.org/reference/functions/get_template/)`wp-includes/theme.php` |

Retrieves name of the active theme.

  | 
| [get_stylesheet_directory()](https://developer.wordpress.org/reference/functions/get_stylesheet_directory/)`wp-includes/theme.php` |

Retrieves stylesheet directory path for the active theme.

  | 
| [get_template_directory()](https://developer.wordpress.org/reference/functions/get_template_directory/)`wp-includes/theme.php` |

Retrieves template directory path for the active theme.

  | 
| [get_stylesheet()](https://developer.wordpress.org/reference/functions/get_stylesheet/)`wp-includes/theme.php` |

Retrieves name of the current stylesheet.

  |

[Show 4 more](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#)

| Used by | Description | 
| [get_block_templates()](https://developer.wordpress.org/reference/functions/get_block_templates/)`wp-includes/block-template-utils.php` |

Retrieves a list of unified template objects based on a query.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_get_block_templates_files/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.3.0](https://developer.wordpress.org/reference/since/6.3.0/) | Added the `$query` parameter. | 
| [5.9.0](https://developer.wordpress.org/reference/since/5.9.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_get_block_templates_files%2F)
before being able to contribute a note or feedback.