WordPress the_search_query() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Prints the search text

Definition

In WordPress, the_search_query() prints the words the visitor searched for. It is used in search.php to say "Results for ..." and in the search form to keep the value.

The text is already escaped, so it is safe to print inside an attribute.

Syntax

A few ways to call the_search_query().

WordPress
<h1>Results for: <?php the_search_query(); ?></h1>
CallWhat it does
the_search_query()Print the escaped search text.
get_search_query()Return the escaped search text.
get_search_query( false )Return the raw text. Escape it yourself.

It prints the search text and takes no arguments. The output is escaped for HTML.

Full declaration
WordPress
the_search_query(): void

Parameters

ParameterDescription
(none)This function takes no parameters.

Return Value

Return ValueDescription
voidNothing is returned. The text is printed.

WordPress Examples

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

Example 1 — Search results heading

Say what the visitor searched for.

WordPress
<h1>Results for: <?php the_search_query(); ?></h1>

How It Works

  1. The visitor searched for wordpress themes.
  2. the_search_query() prints that text.
  3. The output is escaped, so it is safe.

Example 2 — Keep the text in the search box

Put the search text back in the input.

WordPress
<form role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
  <input type="search" name="s" value="<?php the_search_query(); ?>">
  <button type="submit">Search</button>
</form>

How It Works

  1. The form sends the text in the s field.
  2. the_search_query() fills the input with the last search.
  3. On pages that are not search results, the value is empty.

Example 3 — Use the returned text

Use get_search_query() when you need it in code.

WordPress
<?php
$query = get_search_query();
if ( $query ) {
  echo '<p>You searched for <strong>' . $query . '</strong>.</p>';
}

How It Works

  1. get_search_query() returns the escaped text.
  2. The if checks that something was searched.
  3. Because it is escaped, we can print it in HTML.

Frequently Asked Questions

It prints the words that the visitor searched for, escaped for safe output.
Yes. It is escaped, so it is safe to print in HTML and in attributes.
In search.php to show the heading, and in the search form to keep the text.
Use get_search_query().
No. That is not safe. Use the_search_query().

Browse all post functions

Open the functions index for simple guides with examples and FAQs.

Post Functions Index →

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.

9 people found this page helpful