WordPress Template Hierarchy Explained: CPTs, Archives, Taxonomies, and template_include

WordPress template hierarchy: custom post types, archives and taxonomies

For every request WordPress decides which template file of the theme to load by going through a list of names, from the most specific to the most generic, and takes the first file that exists. The child theme is checked before the parent at every step. Knowing this list, you can make a template for one post type, one taxonomy or even one term, without conditions inside single.php.

Problem: The theme’s single.php is full of conditions like if ( 'movie' === get_post_type() ), and it gets harder to maintain with every new post type.

Solution: Move each variant into its own file named according to the template hierarchy (single-movie.php, archive-movie.php), and WordPress loads the right one itself. From a plugin, use the template_include filter.

The lookup chains

The most common ones (WordPress uses the first file it finds):

SINGLE POST (post type 'movie', slug 'the-godfather'):
  single-movie-the-godfather.php
  single-movie.php
  single.php
  singular.php
  index.php

POST TYPE ARCHIVE ('movie', registered with has_archive):
  archive-movie.php
  archive.php
  index.php

TAXONOMY TERM (taxonomy 'genre', term 'action'):
  taxonomy-genre-action.php
  taxonomy-genre.php
  taxonomy.php
  archive.php
  index.php

CATEGORY (slug 'news', ID 3):
  category-news.php
  category-3.php
  category.php
  archive.php
  index.php

AUTHOR (nicename 'john-doe', ID 7):
  author-john-doe.php
  author-7.php
  author.php
  archive.php
  index.php

PAGE (slug 'contact', ID 42):
  the custom template selected in the editor (a file with a "Template Name:" header)
  page-contact.php
  page-42.php
  page.php
  singular.php
  index.php

So for the post type movie it is enough to create single-movie.php and archive-movie.php, and the conditions in single.php are not needed anymore. The repeated parts (header of the post, meta, related posts) go to get_template_part() files shared by these templates.

Templates from a plugin

A plugin that registers a post type can ship its own template. The template_include filter receives the file WordPress has chosen and can return another one. To let themes override the plugin, use the plugin file only if the theme has no template of its own:

<?php
add_filter( 'template_include', 'my_plugin_movie_template' );

function my_plugin_movie_template( $template ) {
    if ( is_singular( 'movie' ) && ! locate_template( 'single-movie.php' ) ) {
        $plugin_template = plugin_dir_path( __FILE__ ) . 'templates/single-movie.php';
        if ( file_exists( $plugin_template ) ) {
            return $plugin_template;
        }
    }
    return $template;
}

locate_template() searches the child and the parent theme and returns an empty string if the file is not there.

NOTE: To see which file was used for the current page, print $GLOBALS['template'] in the footer or use the Query Monitor plugin, which shows the template and the whole list of candidates.