I built a custom search page with a form like:

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">

trouble is I want the results to appear in search.php

whereas I m led to index.php

How can I force to always go to search.php ?

3 Answers 3


If I get it correctly Wordpress loads the search template dynamically by the parameters delivered to it. Specifically the s parameter. Meaning if you try to send the user to the http://www.example.com/search.php you'll get 404 error page.
For now the only workaround I can think is to create a function which is hooked to the init action hook that listens for a parameter in order to load the right file. The function must reside in the functions.php of your theme (assuming you are developing a theme). The function goes like this:

function load_custom_search_template(){
  • This is an excellent idea. I will try it asap. I can't thank you enough already for the valuable input.
    – p.a.
    Sep 22, 2012 at 13:01
  • You're absolutely welcome.
    – 2hamed
    Sep 22, 2012 at 17:02
  • 1
    init is the wrong hook for this.
    – kaiser
    Sep 23, 2012 at 2:09
  • Well I guess there a number of other hooks that can be used for this but init works for me. Simply stating it's wrong will not help anyone. Please explain why you think it's wrong.
    – 2hamed
    Sep 23, 2012 at 5:54
  • It workked ok for me
    – p.a.
    Sep 23, 2012 at 16:48

Template tag for the rescue

Simply use get_search_form( $echo ) for the search form.

This template tag first searches for a searchform.php file in your main theme folder. If there is no such template, then you'll get a default search form. And depending on your input argument, you can echo or just return it for later use. It also has a filter on its end, that contains the complete search form HTML as string: get_search_form with one argument: $form_html.

How and what to use for search form modifications

So if you want to alter the output, just append, prefix, replace whatever...

  • use the get_search_form-filter to pre-/append something or completely replace the form
  • use the custom template to build a unique search form

Or get really sophisticated with theme structure and use the filter to load your searchform from somewhere in your nested theme structure:

// In your functions.php
function wpse65934_searchform_location( $form )
    return get_template_part( 'subfolder/searchform', 'default' );
add_filter( 'get_search_form', 'wpse65934_searchform_location' );
  • Thanks your first proposal worked! I cant use get_search_form because I have a very complex set of criteria, so I design the form myself. I want to allow my users to search posts by * author * tag * category * contained text therefore I build my form from scratch. Many thanks for the help!
    – p.a.
    Sep 22, 2012 at 15:20
  • @p.a. Glad it helped, but please mark the answer as solution: Use the ✓-check mark below the voting arrows on the upper right of this answer. Btw: Search for the posts_clauses-filter in questions (and maybe my name) to see how you can alter the search query properly. If you don't get around by yourself, simply open another question.
    – kaiser
    Sep 22, 2012 at 15:28
  • why would I need to work with posts_clauses? Why not with WP_Query(args) where args contains tags, authors, categories and string criteria? BTW I checked your first answer, which solved my pain.
    – p.a.
    Sep 22, 2012 at 15:31
  • @p.a. Sorry: Which first answer? Ad WP_Query) You add another query, so why not simply alter the original one? That's why WP got this filter: make things quick and keep DB + memory pressure low.
    – kaiser
    Sep 22, 2012 at 16:22
  • can ou refer to posts_clauses article? An introductory one pls
    – p.a.
    Sep 23, 2012 at 16:50

Try adding a hidden input of keyword search, as it'll redirect to the search.php template

<input type="hidden" name="s" id="search" value="<?php the_search_query(); ?>" />

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.