Override the Template Hierarchy

The WordPress template hierarchy is an amazing miracle of programming that seems to hold all of the answers in terms of making a WordPress theme. It is very rare when I encounter an issue where I don’t want to let the Template Hierarchy do what it is meant to do, but I have encountered such instances.

The Use Case:

I created a new Custom Post Type to built a knowledge base with WordPress. This CPT was very much separate from the default  “posts” since we used the posts for blogging and news and the knowledge base for a very specific set of resources, downloads, videos, and articles. I used the traditional template hierarchy to create a unique look for the archive page for this CPT (the page that lists all posts from the CPT) by making a file called archive-knowledge_base.php but then I found that my search results did not use this custom template but instead the default search.php

I found that there is not template hierarchy for search.php to account for custom taxonomies or post types. Instead of creating a number of conditional statements inside of search.php I simply overrode the normal hierarchy to force WordPress to use my archive-knowledge_base.php template to display my results. This keeps all results and lists consistent in their layout.

How to use it:

It’s absolutely simple! We will add something into our functions.php  Check out the code below.

Quick Explaination

This is a very simply snippet of code. First you are getting the $query_var which is a variable from the $wp_query object, in this case the post type. Once we have that we check if WordPress is running a search query and it is for our defined custom post type called ‘knowledge_base’. If both of those things are true, we define the new highest priority template using locate_template. Finally, we take that code and apply it to the template_include filter hook which is applied before WordPress decides which template on the Template Hierarchy rank to use.

Useful References:


About The_Idiot

Self made millionaire.. Oops I mean web developer.. who started out learning how to build websites with tables and dreamweaver, I now almost entirely focus on CMS-based development, usually WordPress. I love coding and designing and regularly switch between my left and right brains.

2 thoughts on “Override the Template Hierarchy

  1. MIchael

    Hey Lindsay,

    This is what I have been looking for for the better part of a week. A simple elegant solution, thanks a ton!


  2. Lenard Jenkinson


    You Need Leads, Sales, Conversions, Traffic for webdesignforidiots.net ?
    I Will Findet…

    Don’t believe me? Since you’re reading this message then you’re living proof that contact form advertising works!
    We can send your ad to people via their Website Contact Form.


    IF YOU ARE INTERESTED, Contact us => lisaf2zw526@gmail.com


Leave a Reply to Lenard Jenkinson Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">