get_block_templates( array $query = array(), string $template_type = 'wp_template' ): WP_Block_Template[]
- Since
- 5.8.0
- Source
wp-includes/block-template-utils.php:1093
Compatibility
- WordPress
- since 5.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$queryarrayoptional- Arguments to retrieve templates.Default:
array()$slug__instring[]List of slugs to include.$wp_idintPost ID of customized template.$areastringA 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only).$post_typestringPost type to get the templates for.
$template_typestringoptional- Template type. Either 'wp_template' or 'wp_template_part'.Default:
'wp_template'
Return value
WP_Block_Template[]- Array of block templates.
Performance profile
How much work a call to get_block_templates() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 11
- Plugin surface
- 2 hooks
- Called by
- 7
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 201.
Third-party callbacks on 'pre_get_block_templates', 'get_block_templates' run inside this call, and their cost is not bounded by anything here.
7 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below get_block_templates() - querycontent query
get_post()one call below get_block_templates()
Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_block_templates() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11 | apply_block_hooks_to_content() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 201 | 11 | 26 | |
| 8.5 | 201 | 11 | 26 | |
| 8.4 | 201 | 11 | 26 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 207 | 11 | 26 | |
| 8.2 | 207 | 11 | 26 | |
| 8.1 | 207 | 11 | 26 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 208 | 11 | 26 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while get_block_templates() runs, in this order:
- apply_filters( pre_get_block_templates )filterline 1113 (+20 into the body)
Filters the block templates array before the query takes place.
- apply_filters( get_block_templates )filterline 1263 (+170 into the body)
Filters the array of queried block templates array after they've been fetched.
Uses · 12
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_stylesheet()Retrieves name of the current stylesheet.
- _build_block_template_result_from_post()Builds a unified template object based a post Object.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- _get_block_templates_files()Retrieves the template files from the theme.
- _build_block_template_result_from_file()Builds a unified template object based on a theme file.
- get_default_block_template_types()Returns a filtered list of default template types, containing their localized titles and descriptions.
- apply_block_hooks_to_content()Runs the hooked blocks algorithm on the given content.
- WP_Query::__construct()Constructor.
- WP_Block_Templates_Registry::get_instance()::get_by_query()
- WP_Block_Templates_Registry::get_instance()Utility method to retrieve the main instance of the class.
Used by · 7
- WP_REST_Templates_Controller::create_item()Creates a single template.
- WP_REST_Templates_Controller::get_items()Returns a list of templates.
- WP_Theme::get_post_templates()Returns the theme's post templates.
- build_template_part_block_instance_variations()Returns an array of instance variation objects for the template part block
- resolve_block_template()Returns the correct 'wp_template' to render for the request template type.
- wp_generate_block_templates_export_file()Creates an export of the current templates and template parts from the site editor at the specified path in a ZIP file.
- wp_get_post_content_block_attributes()Retrieves Post Content block attributes from the current post template.
Source code
function get_block_templates( $query = array(), $template_type = 'wp_template' ) { /** * Filters the block templates array 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_templates Return an array of block templates to short-circuit the default query, * or null to allow WP to run its normal queries. * @param array $query { * Arguments to retrieve templates. All arguments are optional. * * @type string[] $slug__in List of slugs to include. * @type int $wp_id Post ID of customized template. * @type string $area A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only). * @type string $post_type Post type to get the templates for. * } * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. */ $templates = apply_filters( 'pre_get_block_templates', null, $query, $template_type ); if ( ! is_null( $templates ) ) { return $templates; } $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; $wp_query_args = array( 'post_status' => array( 'auto-draft', 'draft', 'publish' ), 'post_type' => $template_type, 'posts_per_page' => -1, 'no_found_rows' => true, 'lazy_load_term_meta' => false, 'tax_query' => array( array( 'taxonomy' => 'wp_theme', 'field' => 'name', 'terms' => get_stylesheet(), ), ), ); if ( 'wp_template_part' === $template_type && isset( $query['area'] ) ) { $wp_query_args['tax_query'][] = array( 'taxonomy' => 'wp_template_part_area', 'field' => 'name', 'terms' => $query['area'], ); $wp_query_args['tax_query']['relation'] = 'AND'; } if ( ! empty( $query['slug__in'] ) ) { $wp_query_args['post_name__in'] = $query['slug__in']; $wp_query_args['posts_per_page'] = count( array_unique( $query['slug__in'] ) ); } // This is only needed for the regular templates/template parts post type 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_block_template_result_from_post( $post ); if ( is_wp_error( $template ) ) { continue; } if ( $post_type && ! $template->is_custom ) { continue; } if ( $post_type && isset( $template->post_types ) && ! in_array( $post_type, $template->post_types, true ) ) {Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/block-template-utils.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.