Home WordPress Links Functions get_search_link() WordPress get_search_link() Function Definition In WordPress, get_search_link() returns the URL of the search results page for a word or phrase. It is the address you would reach after typing that word in the search box.
If you do not pass a word, it uses the current search. Depending on your permalink setting, the address looks like /?s=hooks or /search/hooks/.
Syntax Pass a search word and print the result with esc_url().
<?php echo esc_url( get_search_link( 'hooks' ) ); ?> Call What it does get_search_link( 'hooks' )The search URL for the word hooks. get_search_link()The search URL for the current search. get_search_link( 'wordpress' )The search URL for wordpress.
It returns the URL. It does not run a search by itself.
Full declaration get_search_link( string $query = '' ): string Parameters Parameter Description $querystring. The search term. If empty, WordPress uses the current search term.
Return Value Return Value Description string The URL of the search results page.
WordPress Examples Three short examples. Each one shows the code and what it prints or does.
1. Get a search URL 2. Make a Popular searches list 3. Use the current search Example 1 — Get a search URL Create a link to the results for one word.
<?php
echo esc_url( get_search_link( 'hooks' ) ); https://example.com/search/hooks/ How It Works hooks is the search word.With pretty permalinks the URL looks like /search/hooks/. With plain permalinks it is /?s=hooks. Example 2 — Make a Popular searches list Show links that open a search.
<?php
$words = array( 'hooks', 'themes', 'plugins' );
echo '<ul>';
foreach ( $words as $word ) {
echo '<li><a href="' . esc_url( get_search_link( $word ) ) . '">' . esc_html( $word ) . '</a></li>';
}
echo '</ul>'; <ul><li><a href="https://example.com/search/hooks/">hooks</a></li><li><a href="https://example.com/search/themes/">themes</a></li><li><a href="https://example.com/search/plugins/">plugins</a></li></ul> How It Works We keep a small list of words. get_search_link() turns each word into a URL.Visitors can click a word to run the search. Example 3 — Use the current search On a search page, call it with no argument.
<?php
// Visitor opened /?s=wordpress
echo esc_url( get_search_link() ); https://example.com/?s=wordpress How It Works No word is passed. WordPress reads the current search term. It returns the link for the same search. Frequently Asked Questions What does get_search_link() do? It returns the URL of the search results page for a word you give it.
What if I pass no word? It uses the search term of the current page.
Does it search for me? No. It only builds the URL. The search runs when someone opens it.
Why does the URL look different on my site? It depends on your permalink setting. Plain permalinks use ?s=word. Pretty permalinks use /search/word/.
How do I get the current search word? Use get_search_query().
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
13 people found this page helpful
Helpful Share Copy link Suggestion