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! :)
37 Responses
Perishable – April 29, 2008 •
My pleasure! Thanks for the positive feedback! :)
prof kienstra – May 2, 2008 •
Thanks for the input. I liked your script to return the number of posts found after the search, and will definitely be using it in my next theme.
Perishable – May 4, 2008 •
Excellent — I am glad you found the information useful. Thank you for the positive feedback :)
Shane10101 – May 29, 2008 •
I’m trying to figure out how to have the
searchpage.phppage only display the post titles (& URIs) & the first x words of the returned search results. Could you point me in the right direction?Thanks :)
Perishable – May 31, 2008 •
Have you tried using
<?php the_excerpt(); ?>instead of<?phpthe_content();?>? If the default WordPress excerpt lacks the desired functionality, you may want to check out the excellent plugin, the_excerpt Reloaded. As for the post titles, something like<?php the_title(); ?>should do the trick.angus – November 26, 2008 •
Great overview of WP search. I’m wondering if someone has already written something to return search results as XML. Specifically something that another web page could call (web service, .php page, whatever) that outputs XML and could be incorporated with another website.
Thanks!
Rafael-Brazil – January 19, 2009 •
Hi i have a question, would appreciate a lot if someone could help me in this….
I’m using WP as CMS and works fine everything BUT, when calling the search function having a little trouble, that is: My site is a multi language site, when searching a item, it still bring me to the default language. i have dutch and english, the default language is dutch, so when someone switch to english and want to search for something the result page shows in dutch, i need to add a something like : if( isset($_GET['lang']) ){echo ‘&lang=’.$_GET[lang]; }
Keith – February 3, 2009 •
I want to use dropdowns to search multiple categories like here -> http://www.cbonline.org.au/index.cfm?pageId=13,0,31,0
any suggestions on how to best achieve this?
Thanks!
Kash – April 22, 2009 •
Hi there,
I’ve been desperately trying to do the same as Keith and failing miserably. I also want to be able to filter by 3 category drop downs AND get the wordpress to spit out the search results in order of relevance.
There are a few plugins that are meant to work, but the application of conditional filters seem to break it… any ideas :)
The plugins are:
http://wordpress.org/extend/plugins/wpsearch/
http://wordpress.org/extend/plugins/wordpress-sphinx-plugin/
I would appreciate your thoughts if you have come across there?
Cheers!
Kash
Rebecca – April 28, 2009 •
Dear Jeff,
I want to search one category and its subcategories. SuperSearch plugin is really close to what I want except I don’t want a dropdown of different categories. I want thre to be a built-in function in the search form that only searches one category and its subcategories so that the list is dynamic (rather than manually adding cat=3,4,5)
I would greatly appreciate any advice you can give. Thanks!
alind – September 24, 2009 •
nice article, i am new to wordpress, i want my site search to search blogs also, is there any way to achive it. Any wordpress funciton which will give me a array of links for a given string.
i have integrated the wordpress in my site.
i m not getting any solution for this.
i don’t want to search through database directly by writing a query, is their any built in function.
Thanks
alind
Jeff Starr – September 26, 2009 •
Hi alind, I think the easiest way to search across multiple sites/domains is to configure a custom search engine using Google:
http://www.google.com/cse/
It is so easy, and quite effective.