amarinediary
amarinediary

Reputation: 5449

Custom result page upon submitting search form

I'm running in circle, i'm trying to run a query from a custom search form and to display the result on a specific custom page.

I've created a custom search page called search-custom.php and i've registered it. Now I don't understand why when I submit my seachform i'm getting a 404 error.

This is my searchform currently, upon submitting the form, the url path seems right, am I missing something?

<form action="<?php echo esc_url( home_url().'/search-custom' ); ?>" method="get">
<input type="hidden" name="s" value="<?php the_search_query(); ?>">
<input type="hidden" value="qcm" name="post_type">
<button type="submit"><span class="mr-2">Go</button>
</form>

I don't want to redirect it from the function.php file I need this current format. Thanks in advance for your lights.

Upvotes: 0

Views: 1107

Answers (1)

rank
rank

Reputation: 2534

You do not need to change the action of your form to the name of your template file. Inside of your search-custom.php there is:

<form action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
    <input type="hidden" name="s" value="<?php the_search_query(); ?>">
    <input type="hidden" value="qcm" name="post_type">
    <button type="submit"><span class="mr-2">Go</button>
</form>

With this template file having set, you can use it in your page template (the page you want the form to display, or maybe in a sidebar or something) with:

<?php get_template_part( 'search', 'custom' ); ?>

The default wordpress search template will be used to output the results.

If you want to use a different template file for the results, you can put this in the functions.php file of your theme and change the results page (i.e. search-results-custom.php):

add_action('template_include', 'search_custom_template');

function search_custom_template( $template ) {
  if ( isset( $_REQUEST['search'] ) && is_search() ) {
     $temp = locate_template('search-results-custom.php');
     if ( ! empty($temp) ) {
         $template = $temp;
     }
  }
  return $template;
}

Multiple search results pages using different forms

If you want to set a value in your form, and using that to get to specific result pages:

  1. Open the search.php file of your theme, make a copy for safety and replace the whole content of the file with the following code:

     <?php
     if(isset($_GET['post_type'])) {
         $type = $_GET['post_type'];
         if($type == 'qcm') {
             load_template(TEMPLATEPATH . '/search-custom.php');
         } elseif($type == 'another') {
             load_template(TEMPLATEPATH . '/search-another.php');
         }
     }
     ?>
    

In this code we check what value is set for the field with the name post_type. In your question, you have set the value of qcm. When the field has this value on submit, you will load the search-custom.php. If you have other forms with other values, you can just add an elseif and load another search results template.

If you want to use another hidden input for checking the value, you can simply create one:

<input type="hidden" name="my-hidden-info" value="value-to-check" />

As you see, this way you can edit a value inside of your form and get different search results pages for the specific values. The action of the form is not changed and you will always use the search.php file. You just add other templates, which you are loading inside of this file in the if and elseif. So you get multiple search results pages using different forms.

Upvotes: 4

Related Questions