Home WordPress Posts Functions the_search_query() WordPress the_search_query() Function 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().
<h1>Results for: <?php the_search_query(); ?></h1> Call What 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 Parameters Parameter Description (none)This function takes no parameters.
Return Value Return Value Description void Nothing is returned. The text is printed.
WordPress Examples Three short examples. Each one shows the code and what it prints.
1. Search results heading 2. Keep the text in the search box 3. Use the returned text Example 1 — Search results heading Say what the visitor searched for.
<h1>Results for: <?php the_search_query(); ?></h1> <h1>Results for: wordpress themes</h1> How It Works The visitor searched for wordpress themes. the_search_query() prints that text.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.
<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 The form sends the text in the s field. the_search_query() fills the input with the last search.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.
<?php
$query = get_search_query();
if ( $query ) {
echo '<p>You searched for <strong>' . $query . '</strong>.</p>';
} <p>You searched for <strong>wordpress themes</strong>.</p> How It Works get_search_query() returns the escaped text.The if checks that something was searched. Because it is escaped, we can print it in HTML. Frequently Asked Questions What does the_search_query() do? It prints the words that the visitor searched for, escaped for safe output.
Is the output safe? Yes. It is escaped, so it is safe to print in HTML and in attributes.
Where do I use it? In search.php to show the heading, and in the search form to keep the text.
How do I return the text? Use get_search_query().
Can I print $_GET s directly? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion