Latest TweetsVerify any search engine or visitor via CLI Forward-Reverse Lookup perishablepress.com/cli-forwar…
Perishable Press

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

Jeff Starr
About the Author Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
Archives
37 responses
  1. Jeff Starr

    My pleasure! Thanks for the positive feedback! :)

  2. prof kienstra May 2, 2008 @ 12:58 pm

    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.

  3. Jeff Starr

    Excellent — I am glad you found the information useful. Thank you for the positive feedback :)

  4. Shane10101 May 29, 2008 @ 5:13 pm

    I’m trying to figure out how to have the searchpage.php page only display the post titles (& URIs) & the first x words of the returned search results. Could you point me in the right direction?

    Thanks :)

  5. Jeff Starr

    Have you tried using <?php the_excerpt(); ?> instead of <?php the_content(); ?>? If the default WordPress excerpt lacks the desired functionality, you may want to check out the excellent plugin, the_excerpt Reloaded (404 link removed 2013/10/15). As for the post titles, something like <?php the_title(); ?> should do the trick.

  6. 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!

  7. Rafael-Brazil January 19, 2009 @ 6:35 am

    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]; }

  8. 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!

  9. 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

  10. 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!

  11. 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

  12. Jeff Starr

    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.

[ Comments are closed for this post ]