WordPress is_search() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Conditional tag

Definition

In WordPress, is_search() returns true on a search results page, such as /?s=wordpress. On other pages it returns false.

Use it to print "Search results for ..." headings, or to change how results look.

Syntax

Use it as the test of an if statement.

WordPress
if ( is_search() ) {
  // code for the true path
} else {
  // code for the false path
}
CallWhat it does
is_search()True on a search results page.
get_search_query()Returns the text the visitor searched for.
have_posts()Tells you if the search found anything.

The theme file is search.php. A search with no results still counts as a search page.

Full declaration
WordPress
is_search(): bool

Parameters

ParameterDescription
NoneThis function takes no parameters.

Return Value

Return ValueDescription
booltrue if the main query is a search, otherwise false.

WordPress Examples

Three short theme examples. Each one shows what prints on the true path and the false path.

Example 1 — Search heading

Show the search words in a heading.

WordPress
<?php if ( is_search() ) {
  echo '<h1>Results for: ' . esc_html( get_search_query() ) . '</h1>';
} ?>

How It Works

  1. is_search() is true on search results.
  2. get_search_query() returns the search text.
  3. esc_html() protects the page from bad input.

Example 2 — No results message

Tell the visitor when nothing was found.

WordPress
<?php if ( is_search() && ! have_posts() ) {
  echo '<p>Sorry, nothing matched your search.</p>';
} ?>

How It Works

  1. is_search() checks the page type.
  2. ! have_posts() means there are no posts.
  3. Both parts must be true.

Example 3 — Keep search pages out of Google

Add a noindex tag to search pages.

WordPress
function mytheme_noindex_search() {
  if ( is_search() ) {
    echo '<meta name="robots" content="noindex, follow">';
  }
}
add_action( 'wp_head', 'mytheme_noindex_search' );

How It Works

  1. The wp_head hook runs after the query is set.
  2. is_search() is true on search results.
  3. The meta tag is added only there.

Frequently Asked Questions

It returns true or false (a bool). Use it inside an if statement.
No. is_archive() is false on search results. Use is_search().
Use get_search_query() and escape it with esc_html().
No. It is for the public site search, not the dashboard search.
Conditional tags need the main query. Call them in template files or on hooks such as wp or template_redirect, not directly at the top of functions.php.

Next: is_single()

Check if the current page is a single blog post (not a Page).

is_single() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful