WordPress Search Functionality Documentation
How WordPress searches content and displays the active search term
WordPress Search Functionality
WordPress includes a built-in search feature that lets visitors find content
across the site. By default, the search system looks through posts and pages
that are marked as searchable, checking fields such as the post title, the
main content, and sometimes the excerpt. Themes and plugins can extend this
behavior to include custom post types or additional fields, but the basic
idea is that WordPress performs a keyword-based search against the site’s
database and then returns a list of matching results using the theme’s
search.php template or, if that file does not exist,
index.php as a fallback.
When a user submits a search form, WordPress builds a special query behind the scenes using the search term. That query is used to generate the results loop, which can be customized in the theme to show titles, excerpts, dates, and other metadata about each matching post or page.
The Role of the URI in WordPress Search
The search behavior in WordPress is controlled by the URI (Uniform Resource
Identifier), specifically the query string. When a user performs a search,
WordPress passes the search term in a parameter named s. In a
basic configuration, the search URI might look like:
?s=wordpress+theme. With pretty permalinks enabled, the search
term may appear in a more readable form, such as
/search/wordpress-theme/, but internally WordPress still uses
the s parameter.
The value of the s parameter tells WordPress which keyword or
phrase to search for. Additional query string parameters (for example
post_type=page or paged=2) can further filter or
paginate the results. Because the URI defines the current search context,
any links, pagination, or filters on the results page must preserve these
parameters to keep the search results accurate and consistent.
Showing the Active Search Term in the Search Form
A common usability feature is to keep the current search term visible in the
search input field after the user submits the form. In WordPress, this is
done using the get_search_query() function, which returns the
active search term from the URI. Inside a search form, the value attribute
of the input can be populated with this function so that the user sees the
term they just searched for.
A typical search input in a theme looks like this:
<input type="search" name="s" value="<?php echo esc_attr( get_search_query() ); ?>" />
When no search has been performed, get_search_query() returns
an empty string, so the field appears blank. When a search is active,
WordPress reads the s parameter from the URI and the function
returns that term, ensuring the search form always reflects the current
search context.
Summary of the Documentation
WordPress provides a built-in search system that queries posts, pages, and
other searchable content types for a given keyword or phrase. The search
behavior is driven by the URI: the s parameter carries the
active search term, and additional query parameters can further filter or
paginate the results. Themes display those results using the search template
and can customize how each match is shown.
By using get_search_query() inside the search form, theme
developers can show the current search term directly in the input field,
giving users clear feedback about what they are searching for. Understanding
how the URI, query parameters, and template files work together makes it
easier to build clear, user-friendly search experiences in WordPress.