WordPress Search Function Notes
by Jeff Starr on Wednesday, July 26, 2006
Code to call an external WordPress search form:
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
Code for a standard, inline WordPress search form:
<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><input name="s" type="text" id="s" size="33" maxlength="99" />
<input type="submit" class="submit" value="Search »" /></p>
</form>
Code to search through multiple categories when using customized, individual category pages:
<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><input name="s" type="text" id="s" size="33" maxlength="99" />
<input type="hidden" name="cat" value="1,2,3,4,5,6" />
<input type="submit" class="submit" value="Search »" /></p>
</form>
Code to try if the search function fails and your blog is located in a directory other than root:
<form method="get" id="searchform" action="<?php bloginfo('siteurl'); ?>">
or:
<form method="get" id="searchform" action="http://yourblog.com/blog/index.php">
This particular search function uses the site URL (note the trailing slash) as the form action, and employs a .gif image as the input type:
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" value="Search" name="s" id="s" />
<input type="image" src="<?php bloginfo('stylesheet_directory'); ?>/images/search.gif" id="searchsubmit" value="Search" />
</div></form>
Just for fun, here is bit of php that should (have not checked it yet) output a value indicating the total number of search results:
<?php
$search_count = 0;
$search = new WP_Query("s=$s & showposts=-1");
if($search->have_posts()) : while($search->have_posts()) : $search->the_post();
$search_count++;
endwhile; endif;
echo $search_count;
?>
Another neat trick is to include a default message if some condition is met. For example, here we are telling WordPress to display the text string, "Search Perishable Press", unless someone is actually searching for something:
<?php if (!is_search()) {
$search_text = "Search Perishable Press";
} else {
$search_text = "$s";
} ?>
<form method="get" id="searchform" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" id="s" name="s" value="<?php echo wp_specialchars($search_text, 1); ?>" />
<input type="submit" id="searchsubmit" value="Search!" />
</form>
Here is a core hack to get WordPress to search pages in addition to posts for older (1.5) versions of WordPress (I think newer versions do this automatically). Open the file wp-includes/classes.php and look around line #493 for the following code:
if ($this->is_page) {
$where .= ' AND (post_status = "static")';
} else {
$where .= ' AND (post_status = "publish")';
}
Replace (comment out) that entire chunk of code and replace it with this:
if ($this->is_page) {
$where .= ' AND (post_status = "static")';
} elseif ($this->is_search) {
$where .= ' AND (post_status = "publish" OR post_status = "static")';
} else {
$where .= ' AND (post_status = "publish")';
}
Happy searching! :)


Focused on clean code and quality content, Perishable Press is the online home of Jeff Starr, author, artist, designer, developer, and all-around swell guy. 





Social Media
Some more than others, here are some of my favorites..