solostream

How to Customize Multiple Search Result Pages in WordPress

Rebecca Markowitz | July 23, 2009 | 1 Comment

In the previous post, we discussed how to hack the search function in WordPress to have an additional search form that would search  subcategories. Now that you have 2 or more search forms on your site, you might need to customize the search results. Thanks to this solution, I learned how to create multiple search result pages.

Let’s say we have 2 search forms on our site:

  1. General Site-Wide Search
  2. Recipe Search – searches subcategories of the Recipe Category

The first thing we need to do is tweak the search.php template to use it as a filter that will recognize if a search is coming from the Site-Wide search form or the Recipe Search form.

1. Open Search.php and delete everything.  Add the following code:

<?php
/* Template Name: Search Results */
$search_refer = $_GET["site_section"];
if ($search_refer == ‘recipe’) { load_template(TEMPLATEPATH . ‘/recipe-search.php’); }
elseif ($search_refer == ‘site-search’) { load_template(TEMPLATEPATH . ‘/site-search.php’); }; ?>

2. Open Header.php or wherever the General Site-Wide Search Form is located and add this line:

<input type=”hidden” name=”site_section” value=”site-search” />

The Site-Wide Search form will look something like this:

<form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>
<div id=”search”>
<input type=”text” value=” ” onclick=”this.value=”;” name=”s” id=”s” />
<input type=”hidden” name=”site_section” value=”site-search” />

<input name=”" type=”image” src=”<?php bloginfo(‘stylesheet_directory’); ?>/styles/<?php echo “$style_path”; ?>/search.gif” value=”Go” />
</div><!–/search –>
</form>

3. Open Recipes.php or wherever your second search is and insert this line:

<input type=”hidden” name=”site_section” value=”recipe” />

You can change the value “recipe” to whatever suits your needs. Just make sure it matches the value in search.php.

The Recipe Search form for your second search will look something like this. In my case, this second search is meant to search the subcategories of the Recipe category.  See my previous post to learn about hacking the search function to search subcategories.

<form method=”get” id=”rsearchform” action=”<?php bloginfo(‘home’); ?>/”>
<div id=”rsearch”>
<input type=”text” value=”Recipe Search… ” onclick=”this.value=”;” name=”s” id=”rs” />
<?php $categories = get_categories(‘child_of=11′);
$catlist = ”;
foreach ($categories as $cat) {
$catlist.= $cat->cat_ID.’,';
}
$catlist.’11′;
?>
<input type=”hidden” name=”cat” value=”<?php echo “$catlist”?>” />
<input type=”hidden” name=”site_section” value=”recipe” />
<input name=”" type=”image” src=”<?php bloginfo(‘stylesheet_directory’); ?>/styles/<?php echo “$style_path”; ?>/search.gif” value=”Go” />
</div><!–/search –>
</form>

4. Customize the Search Results Templates

If you recall, in step 1, we added the following to the search.php page.

if ($search_refer == ‘recipe’) { load_template(TEMPLATEPATH . ‘/recipe-search.php’); }
elseif ($search_refer == ‘site-search’) { load_template(TEMPLATEPATH . ‘/site-search.php’); }; ?>

Create recipe-search.php (or whatever you named this template) and customize the layout of the results page to your liking. You can use index.php to start with and customize from there.

Create site-search.php (or whatever you named this template) and customize the layout of the results page to your liking. You can use index.php to start with and customize from there.

Tags: , , , , ,

Category: Code Snippets, WordPress as CMS

About Rebecca Markowitz: Rebecca Markowitz has built over 40 WordPress sites for clients as Web Marketing Manager and WordPress specialist at illuminea. illuminea is a Jerusalem-based boutique web agency. WPGarage shows my dedicated relationship with WordPress over the years - full of love, laughs, tears, growth and strong drinks. L'chaim! View author profile.

Elegant WP Themes

Comments (1)

Trackback URL | Comments RSS Feed

  1. Jesse says:

    This is my favorite newly-discovered website, and I’m really hoping this post is some indication of what the archives look like (I’ll be doing a lot of reading over here in the next few weeks).  Being able to search for particular terms by categories is a HUGE plus, especially for the sort of site I’m working with.  I’m going to see if I can adapt this to work with searching by tags as well, and after a brief look over the code I’m hopeful.  Thanks for starting, maintaining, and making it easy to find this blog!
     
    -Jesse

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.