Getting the current search query in WordPress using the_search_query

The the_search_query function in WordPress is designed to retrieve the search query string that has been used in a WordPress search form. It is primarily used in WordPress search result pages, where it can display the search term that a visitor has entered into a search form.

One of the main functions of the_search_query is to sanitize the search term for safe display in HTML. This means it removes any special characters or code that could potentially be harmful or disruptive if displayed directly. This makes it a key function in maintaining the security and stability of a WordPress site when dealing with user input in search forms.

Another use for the_search_query is in the customization of search result pages. By using this function, developers can customize the display of search results based on the search term used, providing a more tailored user experience.

Parameters Accepted by the_search_query Function

The the_search_query function in WordPress does not accept any parameters.

Return Value of the_search_query Function

The the_search_query function does not return any value.

Examples

How to Display the Search Query in a Search Results Page

if (is_search()) {
 echo '<p>You searched for: ' . get_search_query() . '</p>';
}

In this example, the code checks if the current page is a search results page using the is_search() function. If it is, it will display a paragraph with the text “You searched for: ” followed by the search query. The search query is retrieved using the get_search_query() function.

How to Use the_search_query Function in a Form

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
 <input type="text" value="<?php the_search_query(); ?>" name="s" id="s">
 <input type="submit" id="searchsubmit" value="Search">
</form>

In this example, the the_search_query() function is used to populate the search box with the previous search query. This is useful for letting the user see what they had previously searched for.

How to Display a Custom Message When No Search Query is Entered

if (is_search()) {
 if (get_search_query()) {
 echo '<p>You searched for: ' . get_search_query() . '</p>';
 } else {
 echo '<p>No search query entered.</p>';
 }
}

In this example, the code first checks if the current page is a search results page. If it is, it then checks if a search query was entered using the get_search_query() function. If a query was entered, it displays the search query. If no query was entered, it displays a message saying “No search query entered.”

Conclusion

The the_search_query() function in WordPress is a handy tool for developers working with search functionality. This function retrieves the search query string used in a WordPress search and displays it directly. It is commonly used in search results templates to echo the searched terms back to the user, providing a clear indication of the search parameters that generated the displayed results. Keep in mind that the function does not sanitize the output, therefore it’s important to consider potential security implications when using it.

Related WordPress Functions