Home » Functions » get_search_query

Function Name: get_search_query

WordPress Version: 2.3.0

Description: The get_search_query function is used to retrieve the search query entered by the user on the search page. It returns the search query as a string. This function is commonly used when customizing the search results page or creating custom search functionality.

Usage: To retrieve the search query, simply call the get_search_query function. It does not require any arguments.

Example Usage: Let’s say you want to display the search query entered by the user in a custom search results template. You can use the get_search_query function to achieve this:

$search_query = get_search_query();
if ($search_query) {
    echo "Search results for: " . $search_query;
} else {
    echo "No search query entered.";

In the above example, we first call the get_search_query function and assign its result to the variable $search_query. We then check if a search query is present by using a conditional statement. If a search query exists, we display a message with the search query. Otherwise, we display a message indicating that no search query was entered.

This function is handy when you want to customize the display of search results or perform additional actions based on the search query entered by the user.

Note: The get_search_query function is only applicable on the search results page, and it will not return any value on other pages.

Learn More on

Register an account to save your snippets or go Pro to get more features.