The is_search() WordPress PHP function determines whether the query is for a search.
Usage
if (is_search()) {
// Perform an action when the query is for a search
}
Parameters
- No parameters
More information
See WordPress Developer Resources: is_search()
Examples
Display a message on search pages
// Check if the current query is for a search
if (is_search()) {
echo '**You are currently browsing search results!**';
}
Show search term on search pages
// Check if the current query is for a search
if (is_search()) {
echo '**Searching for:** ' . esc_html(get_search_query());
}
Show a custom message when no search results are found
// Check if the current query is for a search with no results
if (is_search() && !have_posts()) {
echo '**Sorry, no results found for your search. Please try again.**';
}
Apply a custom CSS class to the body on search pages
function custom_body_class($classes) {
// Check if the current query is for a search
if (is_search()) {
$classes[] = 'search-page';
}
return $classes;
}
add_filter('body_class', 'custom_body_class');
Display a search-specific sidebar on search pages
// Check if the current query is for a search
if (is_search()) {
get_sidebar('search');
} else {
get_sidebar();
}