Title: get_search_query
Published: April 25, 2014
Last modified: February 24, 2026

---

# get_search_query( bool $escaped = true ): string

## In this article

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

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

Retrieves the contents of the search WordPress query variable.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/get_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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_search_query/?output_format=md#parameters)󠁿

 `$escaped`booloptional

Whether the result is escaped. 
 Only use when you are later escaping it. Do not
use unescaped.

Default:`true`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_search_query/?output_format=md#return)󠁿

 string

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

    ```php
    function get_search_query( $escaped = true ) {
    	/**
    	 * Filters the contents of the search query variable.
    	 *
    	 * @since 2.3.0
    	 *
    	 * @param mixed $search Contents of the search query variable.
    	 */
    	$query = apply_filters( 'get_search_query', get_query_var( 's' ) );

    	if ( $escaped ) {
    		$query = esc_attr( $query );
    	}
    	return $query;
    }
    ```

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

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

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

Filters the contents of the search query variable.

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

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

Retrieves the value of a query variable in the [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/) class.

  | 
| [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/get_search_query/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_search_query/?output_format=md#)

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

Returns document title for the current page.

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

Displays the contents of the search query variable.

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

Displays the links to the extra feeds such as category feeds.

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

Displays search form.

  | 
| [get_search_link()](https://developer.wordpress.org/reference/functions/get_search_link/)`wp-includes/link-template.php` |

Retrieves the permalink for a search.

  |

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

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

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

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/get_search_query/?output_format=md#comment-content-1350)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/get_search_query/#comment-1350)
 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%2Fget_search_query%2F%23comment-1350)
    Vote results for this note: 2[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%2Fget_search_query%2F%23comment-1350)
 4. **Different usage methods**
 5.     ```php
        <?php echo get_search_query(); ?>
    
        <?php $search_query = get_search_query(); ?>
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_search_query%2F%3Freplytocom%3D1350%23feedback-editor-1350)

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