Title: get_search_link
Published: April 25, 2014
Last modified: February 24, 2026

---

# get_search_link( string $query ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#wp--skip-link--target)

Retrieves the permalink for a search.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#parameters)󠁿

 `$query`stringoptional

The query string to use. If empty the current query is used. Default empty.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#return)󠁿

 string The search permalink.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#source)󠁿

    ```php
    function get_search_link( $query = '' ) {
    	global $wp_rewrite;

    	if ( empty( $query ) ) {
    		$search = get_search_query( false );
    	} else {
    		$search = stripslashes( $query );
    	}

    	$permastruct = $wp_rewrite->get_search_permastruct();

    	if ( empty( $permastruct ) ) {
    		$link = home_url( '?s=' . urlencode( $search ) );
    	} else {
    		$search = urlencode( $search );
    		$search = str_replace( '%2F', '/', $search ); // %2F(/) is not valid within a URL, send it un-encoded.
    		$link   = str_replace( '%search%', $search, $permastruct );
    		$link   = home_url( user_trailingslashit( $link, 'search' ) );
    	}

    	/**
    	 * Filters the search permalink.
    	 *
    	 * @since 3.0.0
    	 *
    	 * @param string $link   Search permalink.
    	 * @param string $search The URL-encoded search term.
    	 */
    	return apply_filters( 'search_link', $link, $search );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/link-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/link-template.php#L1185)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/link-template.php#L1185-L1214)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#hooks)󠁿

 [apply_filters( ‘search_link’, string $link, string $search )](https://developer.wordpress.org/reference/hooks/search_link/)

Filters the search permalink.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#related)󠁿

| Uses | Description | 
| [get_search_query()](https://developer.wordpress.org/reference/functions/get_search_query/)`wp-includes/general-template.php` |

Retrieves the contents of the search WordPress query variable.

  | 
| [user_trailingslashit()](https://developer.wordpress.org/reference/functions/user_trailingslashit/)`wp-includes/link-template.php` |

Retrieves a trailing-slashed string if the site is set for adding trailing slashes.

  | 
| [WP_Rewrite::get_search_permastruct()](https://developer.wordpress.org/reference/classes/wp_rewrite/get_search_permastruct/)`wp-includes/class-wp-rewrite.php` |

Retrieves the search permalink structure.

  | 
| [home_url()](https://developer.wordpress.org/reference/functions/home_url/)`wp-includes/link-template.php` |

Retrieves the URL for the current site where the front end is accessible.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#)

| Used by | Description | 
| [get_search_feed_link()](https://developer.wordpress.org/reference/functions/get_search_feed_link/)`wp-includes/link-template.php` |

Retrieves the permalink for the search results feed.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_search_link/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.0.0](https://developer.wordpress.org/reference/since/3.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_search_link%2F)
before being able to contribute a note or feedback.