Galen Gidman

Front-end and WordPress developer

Custom Post Type Search Results Templates in WordPress

As I was building out the the knowledge base section of the marketing site for my new WordPress church themes project, I came across the need to have a custom search results template for my knowledge base articles (custom post types called kb_article).

I figured that WordPress would have something build-in by default for this like they do with archive-{post_type}.php or single-{post_type}.php, but it turns out that’s not the case. The search results template WordPress looks for is search.php. If that’s not there they revert to archive.php and if that’s absent, index.php.

Limiting Search to a Particular Post Type

Before I go on, I want to quickly cover how to limit your search results to a particular custom post type. Add a hidden input to your search form with a name of post_type and the value attribute set to the name of whatever post type you want to search. In my case, my post type was called kb_article, so my form code looked like this:

Display Custom Results

We’ll name our custom search results templates using the WordPress filename conventions of {template}-{post_type}.php. In our case, that would result in search-kb_article.php. We could just check to see if that post_type=kb_article is set in the URL string and use get_template_part to load it if it is, but let’s go a step further. We’ll check to see if post_type is set in the URL string, and if it is, also check to see if custom search template exists for that post type using the {template}-{post_type}.php naming pattern. If it does, we’ll load it. If not, we’ll continue with the default search template.

So far this has worked well for me. If you have any ideas on how it could be improved, let me know in the comments.


  1. Hey Galen –

    I just wanted to drop you a quick line and thank you for writing about this. I’ve been wrestling with this custom search template problem for a few days now.

    One question for you:

    Do you know how this could be altered to search two post types? For instance, I have a search set up to find both post types ‘attorney’ and ‘paralegal’, but I can only get the custom search results page to display if I use one of them.

    1. This should work for that:

      < ?php
      if ( isset( $_GET['post_type'] ) ) {
        $custom_types = array( 'attorney', 'paralegal' );
        if ( in_array( $_GET['post_type'], $custom_types ) ) {
          echo 'custom results';
      echo 'default results';
      1. thank you for that but i can’t make it work !!
        see my code…
        in form

        in search.php

        if ( isset( $_GET[‘post_type’] ) ) {
        $custom_types = array( ‘video’, ‘sound’ );
        if ( in_array( $_GET[‘post_type’], $custom_types ) ) {
        get_template_part( ‘search-media’ );
        get_template_part( ‘search-normal’ );

        but it return blank page for search

        any help please

  2. Dude…you’re awesome! Such a simple fix. I was trying to pull the post type and use an if else formula to display different code. It’s much cleaner to use the separate template. Thank you for taking the time to share your genius!

  3. Good idea and thanks. But you suggest an easier answer at the beginning. Use the hidden field in the search form, then just create ‘archive-{post_type}.php’ – which WP will natively default to.
    No need for second chunk of code.

    1. “Good idea and thanks. But you suggest an easier answer at the beginning. Use the hidden field in the search form, then just create ‘archive-{post_type}.php’ – which WP will natively default to.
      No need for second chunk of code.”

      No it doesn’t.

      Creating a custom post type archive page named archive-products.php and setting the hidden search field named post_type to products doesn’t cause the results to be displayed in the archive page. The default search.php is still used. I’ve just tested it.

      The archive page is for displaying all posts in that post type, not search results.

  4. This worked to an extent. It displayed results from the custom post type, but after that began to display posts from the general Posts in WordPress.

  5. Hi, how can i customize a search plugin to results only some pages because i am doing a law firm website and i would like my visitors to search for a specific lawyer they would want to see their profile.

  6. Hi Galen.

    I copy pasted the php code you sent but it doesn’t work and after i refresh the webpage the browser can not find the page for whole site. Please how can i fix the problem?

  7. Nice but i would not use post_type as the parameter name. It might be used by wp by default if you want to register it as a query var

Leave a Reply

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