WordPress Search Function Notes
Here is a collection of notes about WordPress search functionality. Note that this post was written a long time ago, so test/verify any code before implementing on a live, production site.
Call/display WP search form
Code to call an external WordPress search form:
<?php get_search_form(); ?>
Code for a standard, inline WordPress search form:
<form method="get" class="search-form" action="/">
<label for="s" class="search-label"><?php _e('Search:', 'shapespace'); ?></label>
<input name="s" class="search-input" id="s" type="text" maxlength="99" placeholder="<?php _e('Search..', 'shapespace'); ?>" value="<?php echo esc_attr(get_search_query()); ?>">
<input type="submit" class="search-submit" value="<?php _e('Search', 'shapespace'); ?>">
</form>
If your WordPress installation is located in a directory other than root, replace the first line with this:
<form method="get" id="searchform" action="<?php echo esc_url(home_url('/')); ?>">
Remember to test well before going live.
Filtered search
Code to search through multiple categories when using customized, individual category pages:
<form method="get" class="search-form" action="/">
<label for="s" class="search-label"><?php _e('Search:', 'shapespace'); ?></label>
<input name="s" class="search-input" id="s" type="text" maxlength="99" placeholder="<?php _e('Search..', 'shapespace'); ?>" value="<?php echo esc_attr(get_search_query()); ?>">
<input type="submit" class="search-submit" value="<?php _e('Search', 'shapespace'); ?>">
<input type="hidden" name="cat" value="1,2,3,4,5,6" />
</form>
Notice the hidden field? Change the value attribute to reflect whichever categories you would like to filter.
Image submit button
This search form employs a .gif
image for the submit button:
<form method="get" class="search-form" action="/">
<label for="s" class="search-label"><?php _e('Search:', 'shapespace'); ?></label>
<input name="s" class="search-input" id="s" type="text" maxlength="99" placeholder="<?php _e('Search..', 'shapespace'); ?>" value="<?php echo esc_attr(get_search_query()); ?>">
<input type="image" src="/search.jpg" class="search-submit" />
</form>
Edit the image input src
with the path to the image you want to use as the submit button.
Display number of search results
Just for fun, here is bit of php that outputs a value indicating the total number of search results:
$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;
Alternately, you can do this:
global $wp_query;
echo $wp_query->found_posts.' results!';
Customize the value attribute
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:
if (!is_search()) {
$search_value = "Search Perishable Press";
} else {
$search_value = esc_attr(get_search_query());;
}
Then you can do this in your search form:
<input type="text" id="s" name="s" value="<?php echo $search_value; ?>" />
Older WP versions
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 to “WordPress Search Function Notes”
I love it when people explain things fully, keep it up.
Dude, im trying to replace the search function and i need some help.
I only need to find
wp_specialchars($s, 1);
On title from post, not comments.
Plz help
I’m trying to do the same thing Kash and Keith above are wanting to do… and your article has come the closest to it yet.
There are multiple, multiple people on the WP forums looking to do this (allow multiple drop downs to filter searches) if you could figure it out this page would become even more popular. :)
@NonStop: I know that can be done, but don’t have the time to work it out. You may want to try the WordPress.org forums and someone may already have a solution. Also check the Plugin Directory.
@Noel: I think I recall DysonHat’s WP Smart Sort Premium plugin (404 link removed 2012/06/11) enable multiple dropdown/sortable menus and other advanced search results. I would start there if nothing else. Sweet gravatar btw :)
Thanks man!
I actually found a “theme” that had the feature I was looking for as a plug-in in the theme. It was worth the $$ just to save the time in figuring it out myself. The theme / plugin was the “broker” theme here: http://gorillathemes.com/broker/
I will bookmark that link you posted as well though!
Much appreciation!
No XML shenanigans. And it works with Categories. I think you could easily adapt it to work with tags and fields though as well. I’m pretty good at hacking around in something once it’s put together… just can’t start it / write it to begin with.
Sort of like I can tell you all sorts of cool Star Wars based stories… but I couldn’t have come up with Vader on my own. ;D haha
Right on, I will keep that handy and maybe look at abstracting the search functionality as a free plugin. Not sure if I can afford it though ;)
Do you know if the search works with categories, tags, and custom fields? It doesn’t require any XML shenanigans, does it?
Oh yeah… seen it and, while it’s hilarious, I’m still amazed at how badly they were able to wear the armor… which was bad to begin with. :D haha
I’m actually the detachment leader for the MEPD (http://forum.mepd.net) – a.k.a. commanding officer for the sandtroopers in the 501st Legion. So yeah, I’m a fan! Good to know you’re one too!
(sorry to high-jack the comments here btw)
Yeah same with me — I am primarily a designer who is good at hacking stuff into submission. And I think the community needs a good “advanced-search” plugin, so hopefully I can set aside a bit of time to work on something.
It’s nice to get a fellow Star Wars fan here at Perishable Press. Have you seen this video: http://www.youtube.com/watch?v=dfDEyLbUSxo It’s pretty good ;)
hi Jeff, first, thanks alot for this post, it sure gives me an additional knowledge about wp function.
I’ve been search on google and some wp forum, but still can’t find the answer how to change the wp search result permalink. like:
domain.com/search/keyword
todomain.com/result/keyword
. (‘search
’ replaced by ‘result
’). Can you help me with this problem Jeff? Thanks before.addition: I can change the search permalink by edit the .httaccess file, the permalink now change to
domain.cm/result/keyword
, but the search result show a 404 not found message. Please help me how to fix this. Thanks again. :)I am looking to incorporate a search in to my site (currently developing on localhost), and am trying to search custom post types (taxonomies?) i have tried the ’search everything’ plug in (and others) but am still only getting results from ‘regular’ posts.
I am sure I need to place something in to the functions.php file but not sure how to call this function from within the search.php file
Any advice?
thnaks