Title: get_template_hierarchy
Published: November 2, 2022
Last modified: May 20, 2026

---

# get_template_hierarchy( string $slug, bool $is_custom = false, string $template_prefix = '' ): string[]

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#wp--skip-link--target)

Gets the template hierarchy for the given template slug to be created.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#description)󠁿

Note: Always add `index` as the last fallback template.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#parameters)󠁿

 `$slug`stringrequired

The template slug to be created.

`$is_custom`booloptional

Indicates if a template is custom or part of the template hierarchy.

Default:`false`

`$template_prefix`stringoptional

The template prefix for the created template.
 Used to extract the main template
type, e.g. in `taxonomy-books` the `taxonomy` is extracted.

Default:`''`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#return)󠁿

 string[] The template hierarchy.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#source)󠁿

    ```php
    function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) {
    	if ( 'index' === $slug ) {
    		/** This filter is documented in wp-includes/template.php */
    		return apply_filters( 'index_template_hierarchy', array( 'index' ) );
    	}
    	if ( $is_custom ) {
    		/** This filter is documented in wp-includes/template.php */
    		return apply_filters( 'page_template_hierarchy', array( 'page', 'singular', 'index' ) );
    	}
    	if ( 'front-page' === $slug ) {
    		/** This filter is documented in wp-includes/template.php */
    		return apply_filters( 'frontpage_template_hierarchy', array( 'front-page', 'home', 'index' ) );
    	}

    	$matches = array();

    	$template_hierarchy = array( $slug );
    	// Most default templates don't have `$template_prefix` assigned.
    	if ( ! empty( $template_prefix ) ) {
    		list( $type ) = explode( '-', $template_prefix );
    		// We need these checks because we always add the `$slug` above.
    		if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) {
    			$template_hierarchy[] = $template_prefix;
    		}
    		if ( $slug !== $type ) {
    			$template_hierarchy[] = $type;
    		}
    	} elseif ( preg_match( '/^(author|category|archive|tag|page)-.+$/', $slug, $matches ) ) {
    		$template_hierarchy[] = $matches[1];
    	} elseif ( preg_match( '/^(taxonomy|single)-(.+)$/', $slug, $matches ) ) {
    		$type           = $matches[1];
    		$slug_remaining = $matches[2];

    		$items = 'single' === $type ? get_post_types() : get_taxonomies();
    		foreach ( $items as $item ) {
    			if ( ! str_starts_with( $slug_remaining, $item ) ) {
    					continue;
    			}

    			// If $slug_remaining is equal to $post_type or $taxonomy we have
    			// the single-$post_type template or the taxonomy-$taxonomy template.
    			if ( $slug_remaining === $item ) {
    				$template_hierarchy[] = $type;
    				break;
    			}

    			// If $slug_remaining is single-$post_type-$slug template.
    			if ( strlen( $slug_remaining ) > strlen( $item ) + 1 ) {
    				$template_hierarchy[] = "$type-$item";
    				$template_hierarchy[] = $type;
    				break;
    			}
    		}
    	}
    	// Handle `archive` template.
    	if (
    		str_starts_with( $slug, 'author' ) ||
    		str_starts_with( $slug, 'taxonomy' ) ||
    		str_starts_with( $slug, 'category' ) ||
    		str_starts_with( $slug, 'tag' ) ||
    		'date' === $slug
    	) {
    		$template_hierarchy[] = 'archive';
    	}
    	// Handle `single` template.
    	if ( 'attachment' === $slug ) {
    		$template_hierarchy[] = 'single';
    	}
    	// Handle `singular` template.
    	if (
    		str_starts_with( $slug, 'single' ) ||
    		str_starts_with( $slug, 'page' ) ||
    		'attachment' === $slug
    	) {
    		$template_hierarchy[] = 'singular';
    	}
    	$template_hierarchy[] = 'index';

    	$template_type = '';
    	if ( ! empty( $template_prefix ) ) {
    		list( $template_type ) = explode( '-', $template_prefix );
    	} else {
    		list( $template_type ) = explode( '-', $slug );
    	}
    	$valid_template_types = array( '404', 'archive', 'attachment', 'author', 'category', 'date', 'embed', 'frontpage', 'home', 'index', 'page', 'paged', 'privacypolicy', 'search', 'single', 'singular', 'tag', 'taxonomy' );
    	if ( in_array( $template_type, $valid_template_types, true ) ) {
    		/** This filter is documented in wp-includes/template.php */
    		return apply_filters( "{$template_type}_template_hierarchy", $template_hierarchy );
    	}
    	return $template_hierarchy;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/block-template-utils.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/block-template-utils.php#L1595)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/block-template-utils.php#L1595-L1685)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#related)󠁿

| Uses | Description | 
| [get_taxonomies()](https://developer.wordpress.org/reference/functions/get_taxonomies/)`wp-includes/taxonomy.php` |

Retrieves a list of registered taxonomy names or objects.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [get_post_types()](https://developer.wordpress.org/reference/functions/get_post_types/)`wp-includes/post.php` |

Gets a list of all registered post type objects.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#)

| Used by | Description | 
| [WP_REST_Templates_Controller::get_template_fallback()](https://developer.wordpress.org/reference/classes/wp_rest_templates_controller/get_template_fallback/)`wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php` |

Returns the fallback template for the given slug.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_template_hierarchy/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.1.0](https://developer.wordpress.org/reference/since/6.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_template_hierarchy%2F)
before being able to contribute a note or feedback.