Using WordPress ‘get_sitemap_url()’ PHP function

The get_sitemap_url() WordPress PHP function retrieves the full URL for a sitemap.

Usage

get_sitemap_url( $name, $subtype_name = '', $page = 1 )

Example

// Get the URL for the main sitemap
$url = get_sitemap_url('sitemap-main');

Output: https://example.com/sitemap-main.xml

Parameters

  • $name (string) – Required. The sitemap name.
  • $subtype_name (string) – Optional. The sitemap subtype name. Default: ”.
  • $page (int) – Optional. The page of the sitemap. Default: 1.

More information

See WordPress Developer Resources: get_sitemap_url()

Examples

Get URL for a Custom Sitemap

Retrieve the URL for a custom sitemap named ‘custom-sitemap’.

// Get the URL for a custom sitemap
$url = get_sitemap_url('custom-sitemap');

Get URL for a Subtype Sitemap

Retrieve the URL for a sitemap with a subtype named ‘products’.

// Get the URL for a subtype sitemap
$url = get_sitemap_url('sitemap', 'products');

Get URL for a Subtype Sitemap with Pagination

Retrieve the URL for the second page of a sitemap with a subtype named ‘blog’.

// Get the URL for a paginated subtype sitemap
$url = get_sitemap_url('sitemap', 'blog', 2);

Get URL for a Sitemap with Custom Prefix

Retrieve the URL for a sitemap with a custom prefix named ‘mysitemap’.

// Get the URL for a sitemap with a custom prefix
$url = get_sitemap_url('mysitemap-main');

Get URL for a Multilingual Sitemap

Retrieve the URL for a sitemap in a specific language, e.g., ‘fr’ (French).

// Get the URL for a multilingual sitemap
$url = get_sitemap_url('sitemap-fr');