get_single_template

The timeline below displays how wordpress function get_single_template has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Retrieves path of single template in current or parent template. Applies to single Posts,
 * single Attachments, and single custom post types.
 *
 * The hierarchy for this template looks like:
 *
 * 1. {Post Type Template}.php
 * 2. single-{post_type}-{post_name}.php
 * 3. single-{post_type}.php
 * 4. single.php
 *
 * An example of this is:
 *
 * 1. templates/full-width.php
 * 2. single-post-hello-world.php
 * 3. single-post.php
 * 4. single.php
 *
 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
 * and {@see '$type_template'} dynamic hooks, where `$type` is 'single'.
 *
 * @since 1.5.0
 * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
 * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
 *              template hierarchy when the post name contains multibyte characters.
 * @since 4.7.0 `{Post Type Template}.php` was added to the top of the template hierarchy.
 *
 * @see get_query_template()
 *
 * @return string Full path to single template file.
 */
function get_single_template()
{
    $object = get_queried_object();
    $templates = array();
    if (!empty($object->post_type)) {
        $template = get_page_template_slug($object);
        if ($template && 0 === validate_file($template)) {
            $templates[] = $template;
        }
        $name_decoded = urldecode($object->post_name);
        if ($name_decoded !== $object->post_name) {
            $templates[] = "single-{$object->post_type}-{$name_decoded}.php";
        }
        $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
        $templates[] = "single-{$object->post_type}.php";
    }
    $templates[] = 'single.php';
    return get_query_template('single', $templates);
}

WordPress Version: 5.1

/**
 * Retrieve path of single template in current or parent template. Applies to single Posts,
 * single Attachments, and single custom post types.
 *
 * The hierarchy for this template looks like:
 *
 * 1. {Post Type Template}.php
 * 2. single-{post_type}-{post_name}.php
 * 3. single-{post_type}.php
 * 4. single.php
 *
 * An example of this is:
 *
 * 1. templates/full-width.php
 * 2. single-post-hello-world.php
 * 3. single-post.php
 * 4. single.php
 *
 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
 * and {@see '$type_template'} dynamic hooks, where `$type` is 'single'.
 *
 * @since 1.5.0
 * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
 * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
 *              template hierarchy when the post name contains multibyte characters.
 * @since 4.7.0 `{Post Type Template}.php` was added to the top of the template hierarchy.
 *
 * @see get_query_template()
 *
 * @return string Full path to single template file.
 */
function get_single_template()
{
    $object = get_queried_object();
    $templates = array();
    if (!empty($object->post_type)) {
        $template = get_page_template_slug($object);
        if ($template && 0 === validate_file($template)) {
            $templates[] = $template;
        }
        $name_decoded = urldecode($object->post_name);
        if ($name_decoded !== $object->post_name) {
            $templates[] = "single-{$object->post_type}-{$name_decoded}.php";
        }
        $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
        $templates[] = "single-{$object->post_type}.php";
    }
    $templates[] = 'single.php';
    return get_query_template('single', $templates);
}

WordPress Version: 4.9

/**
 * Retrieve path of single template in current or parent template. Applies to single Posts,
 * single Attachments, and single custom post types.
 *
 * The hierarchy for this template looks like:
 *
 * 1. {Post Type Template}.php
 * 2. single-{post_type}-{post_name}.php
 * 3. single-{post_type}.php
 * 4. single.php
 *
 * An example of this is:
 *
 * 1. templates/full-width.php
 * 2. single-post-hello-world.php
 * 3. single-post.php
 * 4. single.php
 *
 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
 * and {@see '$type_template'} dynamic hooks, where `$type` is 'single'.
 *
 * @since 1.5.0
 * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
 * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
 *              template hierarchy when the post name contains multibyte characters.
 * @since 4.7.0 {Post Type Template}.php was added to the top of the template hierarchy.
 *
 * @see get_query_template()
 *
 * @return string Full path to single template file.
 */
function get_single_template()
{
    $object = get_queried_object();
    $templates = array();
    if (!empty($object->post_type)) {
        $template = get_page_template_slug($object);
        if ($template && 0 === validate_file($template)) {
            $templates[] = $template;
        }
        $name_decoded = urldecode($object->post_name);
        if ($name_decoded !== $object->post_name) {
            $templates[] = "single-{$object->post_type}-{$name_decoded}.php";
        }
        $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
        $templates[] = "single-{$object->post_type}.php";
    }
    $templates[] = "single.php";
    return get_query_template('single', $templates);
}

WordPress Version: 4.7

/**
 * Retrieve path of single template in current or parent template. Applies to single Posts,
 * single Attachments, and single custom post types.
 *
 * The hierarchy for this template looks like:
 *
 * 1. {Post Type Template}.php
 * 2. single-{post_type}-{post_name}.php
 * 3. single-{post_type}.php
 * 4. single.php
 *
 * An example of this is:
 *
 * 1. templates/full-width.php
 * 2. single-post-hello-world.php
 * 3. single-post.php
 * 4. single.php
 *
 * The template hierarchy is filterable via the {@see 'single_template_hierarchy'} hook.
 * The template path is filterable via the {@see 'single_template'} hook.
 *
 * @since 1.5.0
 * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
 * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
 *              template hierarchy when the post name contains multibyte characters.
 * @since 4.7.0 {Post Type Template}.php was added to the top of the template hierarchy.
 *
 * @see get_query_template()
 *
 * @return string Full path to single template file.
 */
function get_single_template()
{
    $object = get_queried_object();
    $templates = array();
    if (!empty($object->post_type)) {
        $template = get_page_template_slug($object);
        if ($template && 0 === validate_file($template)) {
            $templates[] = $template;
        }
        $name_decoded = urldecode($object->post_name);
        if ($name_decoded !== $object->post_name) {
            $templates[] = "single-{$object->post_type}-{$name_decoded}.php";
        }
        $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
        $templates[] = "single-{$object->post_type}.php";
    }
    $templates[] = "single.php";
    return get_query_template('single', $templates);
}

WordPress Version: 4.4

/**
 * Retrieve path of single template in current or parent template.
 *
 * The template path is filterable via the dynamic {@see '$type_template'} hook,
 * e.g. 'single_template'.
 *
 * @since 1.5.0
 * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
 *
 * @see get_query_template()
 *
 * @return string Full path to single template file.
 */
function get_single_template()
{
    $object = get_queried_object();
    $templates = array();
    if (!empty($object->post_type)) {
        $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
        $templates[] = "single-{$object->post_type}.php";
    }
    $templates[] = "single.php";
    return get_query_template('single', $templates);
}

WordPress Version: 4.3

/**
 * Retrieve path of single template in current or parent template.
 *
 * The template path is filterable via the dynamic {@see '$type_template'} hook,
 * e.g. 'single_template'.
 *
 * @since 1.5.0
 *
 * @see get_query_template()
 *
 * @return string Full path to single template file.
 */
function get_single_template()
{
    $object = get_queried_object();
    $templates = array();
    if (!empty($object->post_type)) {
        $templates[] = "single-{$object->post_type}.php";
    }
    $templates[] = "single.php";
    return get_query_template('single', $templates);
}

WordPress Version: 3.9

/**
 * Retrieve path of single template in current or parent template.
 *
 * The template path is filterable via the 'single_template' hook.
 *
 * @since 1.5.0
 *
 * @see get_query_template()
 *
 * @return string Full path to single template file.
 */
function get_single_template()
{
    $object = get_queried_object();
    $templates = array();
    if (!empty($object->post_type)) {
        $templates[] = "single-{$object->post_type}.php";
    }
    $templates[] = "single.php";
    return get_query_template('single', $templates);
}

WordPress Version: 3.7

/**
 * Retrieve path of single template in current or parent template.
 *
 * @since 1.5.0
 *
 * @return string
 */
function get_single_template()
{
    $object = get_queried_object();
    $templates = array();
    if (!empty($object->post_type)) {
        $templates[] = "single-{$object->post_type}.php";
    }
    $templates[] = "single.php";
    return get_query_template('single', $templates);
}