Latest Tech and Tips

How to Use Multiple Search Forms In WordPress

0

Recently we confirmed you the way to restrict search outcomes for particular submit sorts in WordPress. Now we’re going to present you how one can create completely different/a number of search varieties altogether. This means every kind may be restricted to looking for a particular submit kind. Although this isn’t very laborious it is going to require you to have a primary understanding of WordPress templates.

First, you’ll need some search varieties. Place the next code wherever you need them to be in your weblog:


<kind methodology="get" id="searchform" motion="<?php bloginfo('home'); ?>/">
<enter kind="text" worth="" title="s" id="s" />
<enter kind="hidden" title="search-type" worth="normal" />
<enter title="submit" kind="submit" worth="Go" />
</kind>

In order to specify what kind of search this type can be doing, simply change the worth of the hidden area. Right now it’s set to “normal” however it may be something you need. Next, we want to modify the search.php file. Open it up and change every part in it with this code (copy the prevailing code to your clipboard first, you’ll need it in a minute):


<?php
if(isset($_GET['search-type'])) {
    $kind = $_GET['search-type'];
    if($kind == 'random') {
        load_template(TEMPLATEPATH . '/normal-search.php');
    } elseif($kind == 'books') {
        load_template(TEMPLATEPATH . '/books-search.php');
    }
}
?>

So we can be assuming that you’ve got two search varieties, regular and books. This code is solely redirecting the search to the php file that handles that particular question. Now we simply have to create these recordsdata. So, go forward and create a normal-search.php and books-search.php file (simply change “normal” and “books” with no matter values you’ve got been utilizing).

Now, in normal-search.php copy and paste the next code:


$args = array( 'post_type' => 'submit' );
$args = array_merge( $args, $wp_query->question );
query_posts( $args );

Immediately after this paste the loop code out of your clipboard that you simply copied from the search.php file. Together, this code will search solely your regular weblog posts. Now, within the books-search.php file add this little bit of code and once more paste the loop proper after it:


$args = array( 'post_type' => 'books' );
$args = array_merge( $args, $wp_query->question );
query_posts( $args );

This will trigger WordPress to search just for the customized submit kind of “books”. You can repeat this course of for as many search varieties as you want to.