WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/the search query

Contents

Description

Displays the search query for the current request, if a search was made.

This function can be used safely within HTML attributes (as in the "search box" example, below).

Use get_search_query to retrieve the value.

Usage

 <?php the_search_query(); ?> 

Examples

Show search query in search box

If you have just performed a search, you can show the last query in the search box:

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
  <div>
    <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="Search" />
  </div>
</form>

Show search query in results page

You can display the search string on search result pages

<p>You searched for " <?php echo esc_html( get_search_query( false ) ); ?> ". Here are the results:</p>

Notes

Related

See also index of Function Reference and index of Template Tags.