I have a search form that I want to place in multiple pages (it will be in different header types) I do this by using the 'get search form' function. On my Search form I have radio sections listing two custom post types 'poster' and 'house'.

I have two different search pages for each post type. I want them to be separate because I want to add the search separate search pages on my Navigation e.g. "search for a poster", "search for a house".

Is there a way how I can get the search form to redirect the user to either the "search for a house" page or the "search for a poster" depending on the radio button selected before the entry was submitted?.

Preferably I would like to keep all the necessary coding within the Searcform.php file if possible.

  • What do you mean by poster? A user? A post type? A taxonomy term? – Tom J Nowell Apr 19 '13 at 15:45
  • 'poster' is my post type @TomJNowell – Adzay Apr 22 '13 at 21:43

Hook into 'template_include' and change the template here.

Made up example, not tested:

add_action( 'template_include', 'wpse_96472_search_template' );

function wpse_96472_search_template( $template )
    if ( ! is_search() )
        return $template;

    if ( empty ( $_GET['post_type'] ) )
        return $template;

    if ( 'poster' === $_GET['post_type'] )
        return get_template_directory() . '/poster-search-template.php';

    if ( 'house' === $_GET['post_type'] )
        return get_template_directory() . '/house-search-template.php';

    return $template;

You have to change the template names and the $_GET parameter name of course.

  • Hey @toscho , thanks for your contribution. I found an answer. I was able to do what I wanted using "Onclick events" on the radio buttons in the search form: Below is what I did for the radio button Poster. <label for="radio4"><input name="post_type" type="radio" id="radio4" onclick="document.getElementById('searchform').action='<?php echo site_url(); ?>/postersearch'; document.getElementById('searchsubmit').value='Search For Poster';"/> Events</label></li> I did the same for the House radio button. Thanks – Adzay Apr 21 '13 at 22:23
  • @user28123 Please share your solution with us. You can answer your own question in a real answer. – fuxia Apr 21 '13 at 22:25
  • i updated the answer with my solution @toscho – Adzay Apr 22 '13 at 21:50
  • @user28123 Please never put your solution into the question. Write a real answer – use the answer field for that, then mark this answer as accepted. – fuxia Apr 22 '13 at 21:52
  • damn, guess using this site isn't as easy as I thought it would be @toscho – Adzay Apr 23 '13 at 14:32

This is my Solution, I used Onclick attributes for the radio buttons to change the 'actions' of elements within the form.

<form  id="searchme" action="<?php echo site_url(); ?>/postersearch" method="get">
<ul class=" four columns inline-list offset-by-one">
  <li><label for="radio4"><input name="post_type" CHECKED type="radio" id="radio4" onclick="document.getElementById('searchme').action='<?php echo site_url(); ?>/postersearch'; document.getElementById('searchsubmit').value='Search For Posters';"/> Events</label></li>
  <li><label for="radio5"><input name="post_type"  type="radio" id="radio5" onclick="document.getElementById('searchme').action='<?php echo site_url(); ?>/housesearch'; document.getElementById('searchsubmit').value='Search For Houses';"/> Locations</label></li>
<input id="searchsubmit" type="submit" value="Search For posters"  style="width:100%"/>             

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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