I created a Forum plugin and would like to have a cystomized search result template, I added the following excerpt in the plugin execution file:

add_filter('single_template', 'pagina_topicos');

function pagina_topicos($single) {
    global $wp_query, $post;

/* Informando o Modelo de Página das Aulas */
if ($post->post_type == "stopicos"){
    if(file_exists(plugin_dir_path( __FILE__ ) . '/single-topico.php'))
        return plugin_dir_path( __FILE__ ) . '/single-topico.php';

  if( $wp_query->is_search && $post_type == 'stopicos' )
    return plugin_dir_path( __FILE__ ) . '/archive-stopicos.php';

    return $single;

I use the following form in the search:

 <form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
                    <input type="text" class="submit-search form-control input-lg" placeholder="Buscar" />
                    <input type="hidden" name="post_type" value="stopicos"/>
                    <span class="input-group-btn">
                        <button class="btn btn-info btn-lg" type="submit" value="Search">
                            <i class="glyphicon glyphicon-search"></i>

What can be wrong so that the model I customized is not loaded?

  • you check $post_type, but where do you set it?
    – Milo
    Aug 9, 2017 at 16:01

2 Answers 2


You're using the single_template hook, which is called for a single post. From the Codex:

The "single_template" filter can be used to load a custom template for a given post. It will replace the template used whenever the "single" template is called.

My guess is that this can probably be achieved by using the template_include hook:

This filter hook is executed immediately before WordPress includes the predetermined template file. This can be used to override WordPress's default template behavior.

  • There's also search_template, which is specific to just search queries.
    – Milo
    Aug 11, 2017 at 16:17

Zedejose really worked out with template_include, below what I did and it worked


function my_custom_search_template($template){
    global $wp_query;
    if (!$wp_query->is_search)
        return $template;

    return dirname( __FILE__ ) . '/search-stopicos.php';


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.