WordPress get_search_query() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Returns a string

Definition

In WordPress, get_search_query() returns the word a visitor searched for. If the visitor opens /?s=hooks, the function returns hooks.

You use it on the search results page, for example to show Results for: hooks. By default, the value is already escaped, so it is safe to print.

Syntax

Use it in search.php.

WordPress
<?php echo get_search_query(); ?>
CallWhat it does
get_search_query()The search word, escaped.
get_search_query( false )The search word as typed. Escape it yourself.
esc_html( get_search_query( false ) )The safe way to print the raw value.

It returns the word. WordPress also has the_search_query() to print it.

Full declaration
WordPress
get_search_query( bool $escaped = true ): string

Parameters

ParameterDescription
$escapedbool. If true (default), the value is escaped for HTML. If false, you get the raw text.

Return Value

Return ValueDescription
stringThe search word. It is an empty string when there is no search.

WordPress Examples

Three short examples. Each one shows the code and what it prints or does.

Example 1 — Show a results heading

A classic use in search.php.

WordPress
<?php
// Visitor opened /?s=hooks
echo '<h1>Results for: ' . get_search_query() . '</h1>';

How It Works

  1. WordPress reads hooks from the URL.
  2. get_search_query() returns it.
  3. It is already escaped, so you can print it right away.

Example 2 — Fill the search box

Keep the typed word inside the search field.

WordPress
<?php
echo '<input type="search" name="s" value="' . get_search_query() . '">';

How It Works

  1. The function returns the current word.
  2. It goes inside the value attribute.
  3. The escaping stops quotes from breaking the HTML.

Example 3 — Escape the raw value yourself

Say the visitor typed <b>hi</b>. Both lines below print it safely.

WordPress
<?php
echo get_search_query();
echo esc_html( get_search_query( false ) );

How It Works

  1. The first line uses the default escaped value.
  2. The second line takes the raw value and escapes it with esc_html().
  3. Both show the text as plain characters, not as HTML.

Frequently Asked Questions

It returns the word the visitor searched for, so you can show it on the results page.
By default, yes. It is escaped. If you pass false, escape it yourself with esc_html().
An empty string.
get_search_query() returns the word. the_search_query() prints it.
In search.php, or any template that runs on the search results page.

Next: get_site_url()

Return the site URL (where WordPress is installed) for a site, with an optional path.

get_site_url() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful