Title: get_page_template_slug
Published: April 25, 2014
Last modified: May 20, 2026

---

# get_page_template_slug( int|WP_Post|null $post = null ): string|false

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#user-contributed-notes)

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

Gets the specific template filename for a given post.

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

 `$post`int|[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
|nulloptional

Post ID or [WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
object. Default is global $post.

Default:`null`

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

 string|false Page template filename. Returns an empty string when the default page
template is in use. Returns false if the post does not exist.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#more-information)󠁿

The filename of a Page’s assigned custom template is stored as the value of a [Custom Field](https://wordpress.org/support/article/custom-fields/)
with a key named '_wp_page_template' (in the [wp_postmeta](https://codex.wordpress.org/Database_Description#Table:_wp_postmeta)
database table). If the template is stored in a Theme’s subdirectory (or a Parent
Theme’s subdirectory of a Child Theme), the value of the wp_postmeta is both the
folder and file names, e.g.

    ```php
    my-templates/my-custom-template.php
    ```

The function [get_page_template_slug()](https://developer.wordpress.org/reference/functions/get_page_template_slug/)
returns an empty string when the value of '_wp_page_template' is either empty or'
default'.

Custom fields starting with an underscore do _not_ display in the Edit screen’s 
Custom Fields module. To retrieve a Page’s custom template metadata, you can also
use:

    ```php
    get_post_meta( $post->ID, '_wp_page_template', true )
    ```

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

    ```php
    function get_page_template_slug( $post = null ) {
    	$post = get_post( $post );

    	if ( ! $post ) {
    		return false;
    	}

    	$template = get_post_meta( $post->ID, '_wp_page_template', true );

    	if ( ! $template || 'default' === $template ) {
    		return '';
    	}

    	return $template;
    }
    ```

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

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

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

Retrieves a post meta field for the given post ID.

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

Retrieves post data given a post ID or post object.

  |

| Used by | Description | 
| [wp_get_post_content_block_attributes()](https://developer.wordpress.org/reference/functions/wp_get_post_content_block_attributes/)`wp-includes/block-editor.php` |

Retrieves Post Content block attributes from the current post template.

  | 
| [WP_REST_Posts_Controller::check_template()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/check_template/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Checks whether the template is valid for the given post.

  | 
| [WP_REST_Posts_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Prepares a single post output for response.

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

Retrieves path of single template in current or parent template. Applies to single Posts, single Attachments, and single custom post types.

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

Retrieves path of page template in current or parent template.

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

Determines whether the current post uses a page template.

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

Retrieves an array of the class names for the body element.

  | 
| [wp_xmlrpc_server::_prepare_page()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/_prepare_page/)`wp-includes/class-wp-xmlrpc-server.php` |

Prepares page data for return in an XML-RPC object.

  | 
| [_WP_Editors::editor_settings()](https://developer.wordpress.org/reference/classes/_wp_editors/editor_settings/)`wp-includes/class-wp-editor.php` |  |

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

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

| Version | Description | 
| [4.7.0](https://developer.wordpress.org/reference/since/4.7.0/) | Now works with any post type, not just pages. | 
| [3.4.0](https://developer.wordpress.org/reference/since/3.4.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#comment-content-803)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/get_page_template_slug/#comment-803)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_page_template_slug%2F%23comment-803)
     Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_page_template_slug%2F%23comment-803)
 4.  Display the page template filename of the current page:
 5.      ```php
         <?php echo esc_html( get_page_template_slug( $post->ID ) ); ?>
         ```
     
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_page_template_slug%2F%3Freplytocom%3D803%23feedback-editor-803)
 7.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/get_page_template_slug/?output_format=md#comment-content-4310)
 8.    [Ivijan-Stefan Stipic](https://profiles.wordpress.org/ivijanstefan/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/get_page_template_slug/#comment-4310)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_page_template_slug%2F%23comment-4310)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_page_template_slug%2F%23comment-4310)
 10. If you need reverse engineering to find all the pages that are working under a
     particular page template filename, this is one solution that may work for you.
 11.     ```php
         function wpdocs_get_pages_by_template_filename( $page_template_filename ) {
         	return get_pages( array(
         		'meta_key' => '_wp_page_template',
         		'meta_value' => $page_template_filename
         	) );
         }
         ```
     
 12. You can use this function for example:
 13.     ```php
         $pages = wpdocs_get_pages_by_template_filename( 'templates/offers.php' );
         ```
     
 14. And it will return (array|false) list of pages matching by that page template 
     filename.
 15. Generally, it happens that inside a theme you build, you need to find a certain
     page that works under a special custom template and that you need to dynamically
     access its ID, content, title, etc, and this function will help for that.
 16.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_page_template_slug%2F%3Freplytocom%3D4310%23feedback-editor-4310)

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