wppaste
WordPress

WP_Theme::get_post_templates(): array[]

Since
4.7.0, 5.8.0
Source
wp-includes/class-wp-theme.php:1319
Returns the theme's post templates.

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.

Return value

array[]
Array of page template arrays, keyed by post type and filename, with the value of the translated header name.

Performance profile

How much work a call to WP_Theme::get_post_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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
10–51

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 131.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • hookthird-party callbacksapply_filters()one call below WP_Theme::get_post_templates()

Further down the call graph this can also reach option, query, 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 · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme::get_post_templates() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
->errors()10->errors(), ->errors(), ->get_error_codes()
!->errors() && is_array($post_templates) && !current_theme_supports()17–20->errors(), ->cache_get(), current_theme_supports(), ->load_textdomain()
->errors() && is_array($post_templates) && !current_theme_supports()23–26->errors(), ->errors(), ->get_error_codes(), ->cache_get(), current_theme_supports(), ->load_textdomain()
!->errors() && is_array($post_templates) && current_theme_supports()27–31->errors(), ->cache_get(), current_theme_supports(), get_block_templates(), get_post_types(), ->load_textdomain()
!->errors() && !is_array($post_templates) && !current_theme_supports()30–34->errors(), ->cache_get(), ->get_files(), ->cache_add(), current_theme_supports(), ->load_textdomain()
->errors() && is_array($post_templates) && current_theme_supports()33–37->errors(), ->errors(), ->get_error_codes(), ->cache_get(), current_theme_supports(), get_block_templates(), get_post_types(), ->load_textdomain()
->errors() && !is_array($post_templates) && !current_theme_supports()36–40->errors(), ->errors(), ->get_error_codes(), ->cache_get(), ->get_files(), ->cache_add(), current_theme_supports(), ->load_textdomain()
!->errors() && !is_array($post_templates) && current_theme_supports()40–45->errors(), ->cache_get(), ->get_files(), ->cache_add(), current_theme_supports(), get_block_templates(), get_post_types(), ->load_textdomain()
->errors() && !is_array($post_templates) && current_theme_supports()46–51->errors(), ->errors(), ->get_error_codes(), ->cache_get(), ->get_files(), ->cache_add(), current_theme_supports(), get_block_templates(), get_post_types(), ->load_textdomain()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev13110–5123
8.513110–5123
8.413110–51233 fewer instructions than PHP 8.3
8.313410–5123
8.213410–5123
8.113410–5123
7.413410–5123

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.

Uses · 11

Used by · 1

Source code

	public function get_post_templates() {		// If you screw up your active theme and we invalidate your parent, most things still work. Let it slide.		if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) {			return array();		} 		$post_templates = $this->cache_get( 'post_templates' ); 		if ( ! is_array( $post_templates ) ) {			$post_templates = array(); 			$files = (array) $this->get_files( 'php', 1, true ); 			foreach ( $files as $file => $full_path ) {				if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) {					continue;				} 				$types = array( 'page' );				if ( preg_match( '|Template Post Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) {					$types = explode( ',', _cleanup_header_comment( $type[1] ) );				} 				foreach ( $types as $type ) {					$type = sanitize_key( $type );					if ( ! isset( $post_templates[ $type ] ) ) {						$post_templates[ $type ] = array();					} 					$post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] );				}			} 			$this->cache_add( 'post_templates', $post_templates );		} 		if ( current_theme_supports( 'block-templates' ) ) {			$block_templates = get_block_templates( array(), 'wp_template' );			foreach ( get_post_types( array( 'public' => true ) ) as $type ) {				foreach ( $block_templates as $block_template ) {					if ( ! $block_template->is_custom ) {						continue;					} 					if ( isset( $block_template->post_types ) && ! in_array( $type, $block_template->post_types, true ) ) {						continue;					} 					$post_templates[ $type ][ $block_template->slug ] = $block_template->title;				}			}		} 		if ( $this->load_textdomain() ) {			foreach ( $post_templates as &$post_type ) {				foreach ( $post_type as &$post_template ) {					$post_template = $this->translate_header( 'Template Name', $post_template );				}			}		} 		return $post_templates;	}

Changelog

Introduced in 4.7.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.

5.8.0
Include block templates.from the docblock
4.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/class-wp-theme.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.