# get_block_template()

URL: https://chugunov.pro/api-wordpress/functions/get_block_template/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 5.8.0.

## Сигнатура

```php
get_block_template( string $id, string $template_type = 'wp_template' ): WP_Block_Template|null
```

## Описание

Получает один унифицированный объект шаблона по его идентификатору.

## Параметры

- `$id` `string` — обязательный. Уникальный идентификатор шаблона (пример: 'theme_slug//template_slug').
- `$template_type` `string` — необязательный, по умолчанию `'wp_template'`. Тип шаблона. Либо 'wp_template', либо 'wp_template_part'.
  
  Значение по умолчанию — 'wp_template'.

## Возвращаемое значение

`WP_Block_Template|null`

## Исходный код

Файл: `wp-includes/block-template-utils.php:1288`

```php
function get_block_template( $id, $template_type = 'wp_template' ) {
	/**
	 * Filters the block template object before the query takes place.
	 *
	 * Return a non-null value to bypass the WordPress queries.
	 *
	 * @since 5.9.0
	 *
	 * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query,
	 *                                               or null to allow WP to run its normal queries.
	 * @param string                 $id             Template unique identifier (example: 'theme_slug//template_slug').
	 * @param string                 $template_type  Template type. Either 'wp_template' or 'wp_template_part'.
	 */
	$block_template = apply_filters( 'pre_get_block_template', null, $id, $template_type );
	if ( ! is_null( $block_template ) ) {
		return $block_template;
	}

	$parts = explode( '//', $id, 2 );
	if ( count( $parts ) < 2 ) {
		return null;
	}
	list( $theme, $slug ) = $parts;
	$wp_query_args        = array(
		'post_name__in'  => array( $slug ),
		'post_type'      => $template_type,
		'post_status'    => array( 'auto-draft', 'draft', 'publish', 'trash' ),
		'posts_per_page' => 1,
		'no_found_rows'  => true,
		'tax_query'      => array(
			array(
				'taxonomy' => 'wp_theme',
				'field'    => 'name',
				'terms'    => $theme,
			),
		),
	);
	$template_query       = new WP_Query( $wp_query_args );
	$posts                = $template_query->posts;

	if ( count( $posts ) > 0 ) {
		$template = _build_block_template_result_from_post( $posts[0] );

		if ( ! is_wp_error( $template ) ) {
			return $template;
		}
	}

	$block_template = get_block_file_template( $id, $template_type );

	/**
	 * Filters the queried block template object after it's been fetched.
	 *
	 * @since 5.9.0
	 *
	 * @param WP_Block_Template|null $block_template The found block template, or null if there isn't one.
	 * @param string                 $id             Template unique identifier (example: 'theme_slug//template_slug').
	 * @param string                 $template_type  Template type. Either 'wp_template' or 'wp_template_part'.
	 */
	return apply_filters( 'get_block_template', $block_template, $id, $template_type );
}
```

## История изменений

- 5.8.0 — Introduced.

## Связанные

Использует: [`get_block_file_template`](https://chugunov.pro/api-wordpress/functions/get_block_file_template/), [`_build_block_template_result_from_post`](https://chugunov.pro/api-wordpress/functions/_build_block_template_result_from_post/), `WP_Query::__construct`, [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`is_wp_error`](https://chugunov.pro/api-wordpress/functions/is_wp_error/).
Используется в: `WP_REST_Template_Revisions_Controller::get_parent`, [`block_template_part`](https://chugunov.pro/api-wordpress/functions/block_template_part/), `WP_REST_Templates_Controller::prepare_links`, `WP_REST_Templates_Controller::get_item`, `WP_REST_Templates_Controller::update_item`, `WP_REST_Templates_Controller::create_item`, `WP_REST_Templates_Controller::delete_item`, `WP_REST_Templates_Controller::prepare_item_for_database`.

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/get_block_template/
