The get_search_feed_link() WordPress PHP function retrieves the permalink for the search results feed.
Usage
get_search_feed_link( $search_query, $feed );
Custom example:
echo get_search_feed_link('WordPress', 'rss2');
Output:
https://example.com/?s=WordPress&feed=rss2
Parameters
$search_query
(string) Optional: The search query. Default:''
$feed
(string) Optional: The feed type. Possible values include'rss2'
,'atom'
. Default is the value ofget_default_feed()
. Default:''
More information
See WordPress Developer Resources: get_search_feed_link
Examples
Basic Usage
Get the search feed link for the search query “WordPress” with the default feed type.
$search_query = 'WordPress'; echo get_search_feed_link($search_query);
RSS2 Feed Type
Get the search feed link for the search query “WordPress” using the ‘rss2’ feed type.
$search_query = 'WordPress'; $feed_type = 'rss2'; echo get_search_feed_link($search_query, $feed_type);
Atom Feed Type
Get the search feed link for the search query “WordPress” using the ‘atom’ feed type.
$search_query = 'WordPress'; $feed_type = 'atom'; echo get_search_feed_link($search_query, $feed_type);
Search Feed Link in a Template
Display the search feed link for the search query “WordPress” using the default feed type in an HTML anchor tag.
$search_query = 'WordPress'; $link = get_search_feed_link($search_query); echo '<a href="' . esc_url($link) . '">Search Feed</a>';
Search Feed Link with a Custom Feed Type
Get the search feed link for the search query “WordPress” using a custom feed type called “custom_feed”.
$search_query = 'WordPress'; $feed_type = 'custom_feed'; echo get_search_feed_link($search_query, $feed_type);