Spring Sale! Save 30% on all books w/ code: PLANET24
Web Dev + WordPress + Security

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! :)

About the Author
Jeff Starr = Web Developer. Book Author. Secretly Important.
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »

37 responses to “WordPress Search Function Notes”

  1. it work good. thanks your posts.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
The Tao of WordPress: Master the art of WordPress.
Thoughts
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.