get_block_templates( array $query = array(), string $template_type = 'wp_template' )
Retrieves a list of unified template objects based on a query.
Parameters
- $query
-
(array) (Optional) Arguments to retrieve templates.
-
'slug__in'
(array) List of slugs to include. -
'wp_id'
(int) Post ID of customized template.
Default value: array()
-
'slug__in'
- $template_type
-
(string) (Optional) The template type (post type).
Default value: 'wp_template'
Return
(WP_Block_Template[]) Block template objects.
Source
File: wp-includes/block-template-utils.php
function get_block_templates( $query = array(), $template_type = 'wp_template' ) {
$wp_query_args = array(
'post_status' => array( 'auto-draft', 'draft', 'publish' ),
'post_type' => $template_type,
'posts_per_page' => -1,
'no_found_rows' => true,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => wp_get_theme()->get_stylesheet(),
),
),
);
if ( isset( $query['slug__in'] ) ) {
$wp_query_args['post_name__in'] = $query['slug__in'];
}
// This is only needed for the regular templates CPT listing and editor.
if ( isset( $query['wp_id'] ) ) {
$wp_query_args['p'] = $query['wp_id'];
} else {
$wp_query_args['post_status'] = 'publish';
}
$template_query = new WP_Query( $wp_query_args );
$query_result = array();
foreach ( $template_query->posts as $post ) {
$template = _build_template_result_from_post( $post );
if ( ! is_wp_error( $template ) ) {
$query_result[] = $template;
}
}
return $query_result;
} Changelog
| Version | Description |
|---|---|
| 5.8.0 | Introduced. |
© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_block_templates