WordPress Search Function Notes
Post #168 categorized as Function, WordPress, last updated on Feb 6, 2008
Tagged with notes, php, search, WordPress
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! :)
Share this..
Related articles
- Auto Clear and Restore Form Elements
- Search Engine Registration Notes
- Auto-Focus Form Elements with JavaScript
- Fully Valid, SEO-Friendly Social Media Links for WordPress
- Unobtrusive JavaScript: Auto-Clear and Restore Multiple Form Inputs on Focus
- WordPress Notes Plus
- Customize Password-Protected Posts
#1 — Roy
Hi. I have been asking all around, but my question seems too stupid for anyone to answer it. Now that I run into your overview of WP search function codes, I’ll just give it another try.
I’m trying to change the WP search function so, that it (optional if possible) searches only the post titles. Can this be done by changing the code, do I need a search.php (my classic template doesn’t have one and so far I never needed one) or is there another option?
Thanks in advance.