Title: the_search_query
Published: April 25, 2014
Last modified: August 20, 2026

---

# the_search_query()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#wp--skip-link--target)

Displays the contents of the search query variable.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#description)󠁿

The search query string is passed through [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)
to ensure that it is safe for placing in an HTML attribute.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#source)󠁿

    ```php
    function the_search_query() {
    	/**
    	 * Filters the contents of the search query variable, for display.
    	 *
    	 * @since 2.3.0
    	 *
    	 * @param mixed $search Contents of the search query variable.
    	 */
    	echo esc_attr( apply_filters( 'the_search_query', get_search_query( false ) ) );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/general-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/general-template.php#L4722)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/general-template.php#L4722-L4731)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#hooks)󠁿

 [apply_filters( ‘the_search_query’, mixed $search )](https://developer.wordpress.org/reference/hooks/the_search_query/)

Filters the contents of the search query variable, for display.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#related)󠁿

| Uses | Description | 
| [get_search_query()](https://developer.wordpress.org/reference/functions/get_search_query/)`wp-includes/general-template.php` |

Retrieves the contents of the search WordPress query variable.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#)

| Used by | Description | 
| [media_upload_library_form()](https://developer.wordpress.org/reference/functions/media_upload_library_form/)`wp-admin/includes/media.php` |

Outputs the legacy media upload form for the media library.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.1.0](https://developer.wordpress.org/reference/since/2.1.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#comment-content-607)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/the_search_query/#comment-607)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_search_query%2F%23comment-607)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_search_query%2F%23comment-607)
 4.  **Show search query in search box**
      If you have just performed a search, you 
     can show the last query in the search box:
 5.      ```php
         <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>
         ```
     
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_search_query%2F%3Freplytocom%3D607%23feedback-editor-607)
 7.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/the_search_query/?output_format=md#comment-content-608)
 8.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/the_search_query/#comment-608)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_search_query%2F%23comment-608)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_search_query%2F%23comment-608)
 10. **Show search query in results page**
      You can display the search string on search
     result pages
 11.     ```php
         <p>You searched for " <?php echo esc_html( get_search_query( false ) ); ?> ". Here are the results:</p>
         ```
     
 12.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_search_query%2F%3Freplytocom%3D608%23feedback-editor-608)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_search_query%2F)
before being able to contribute a note or feedback.