wppaste
WordPress

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
Retrieves a list of unified template objects based on a query.

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_idint

    Post ID of customized template.
  • $areastring

    A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only).
  • $post_typestring

    Post 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

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
11

Executed per call on PHP 8.5. The body compiles to 201.

Plugin surface
2 hooks

Third-party callbacks on 'pre_get_block_templates', 'get_block_templates' run inside this call, and their cost is not bounded by anything here.

Called by
7

7 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below get_block_templates()
  • querycontent queryget_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.

WhenInstructionsCalls it makes
always11apply_block_hooks_to_content()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2011126
8.52011126
8.420111266 fewer instructions than PHP 8.3
8.32071126
8.22071126
8.120711261 fewer instruction than PHP 7.4
7.42081126

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:

  1. apply_filters( pre_get_block_templates )filterline 1113 (+20 into the body)

    Filters the block templates array before the query takes place.

  2. 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

Used by · 7

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.