How to Check if the Current Page is WordPress Search Page

5 from 5
from 2 user
(Rate this post)

WordPress developers can detect Search Page using the is_search function. is_search function detects if the current page in the user browser is the website search page or not. The result of running the is_search is a Boolean, and it accepts no parameters. Let's try this function with an example.

Check if the current page is a Search Page using the is_search function

Since WordPress v1.5.0, developers can execute the is_search function to determine if the current user is viewing the search page or not.

This function checks if the global wp_query is for a search or not, which helps you detect the search page of WordPress.

is_search function syntax

This function does not accept any parameter and returns true if a search query is detected on the current page.

is_search()

is_search function example

In this example, we’ll check if the current page in the browser is the WordPress search page.

“We looked at every article and found these results for you.” will be printed for the visitor If the search page is presented.

if(is_search()){
    echo "We looked at every article and found these results for you.";
}

If you need to get the searched query, you can use $_GET['s'] variable available from the current URL.

Example of printing searched query if it is the search page:

if(is_search() && isset($_GET['s'])){
    echo "We looked at every article for `".$_GET['s']."` and found these results for you.";
}

What do you think about "How to Check if the Current Page is WordPress Search Page"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu