Currently Available: Need a skilled Software Developer for your next project?
Categories
WordPress

Adding Custom URLs to Your Yoast XML Sitemap

If you use WordPress with Yoast SEO, you may need to list a URL that Yoast does not include automatically. An XML sitemap is a file that lists URLs for search engines to discover. Yoast creates a sitemap index, which points to smaller sitemap files for content such as pages and posts.

Choose based on where the URLs come from. For a few stable URLs, use a WordPress filter, which lets code change sitemap content, to add them to a Yoast sitemap. For content that grows, manage it as WordPress pages or a custom post type, a WordPress content type for a distinct collection. For a large collection from another service, link its sitemap file from Yoast’s index. A sitemap helps search engines find URLs; its entries do not guarantee that search engines will index those pages.

Choose where the URLs belong

Your URLs Recommended approach
A few stable URLs that are not WordPress posts or pages Append URL entries to a Yoast sitemap with a content filter
Pages maintained in WordPress that will grow or change Use pages or a custom post type and enable that content type in Yoast
A large collection generated by another service Add that service’s sitemap file to Yoast’s sitemap index

An individual URL entry and a reference to a sitemap file serve different purposes. A Yoast content filter adds <url> entries to a specific sitemap. The wpseo_sitemap_index filter adds a <sitemap> reference to Yoast’s index, which directs search engines to another sitemap file. Yoast documents both options in its XML sitemap API.

Before writing code, open Yoast’s XML sitemap and check whether the URL already appears. Then review Yoast SEO → Settings → Content types. Yoast includes a content type in its sitemap when it is enabled for search results, and it excludes content marked noindex, as described in its sitemap settings instructions. If a plugin or page builder stores the URL as public WordPress content, Yoast may already include it.

Add a few URLs to a Yoast sitemap

For a small, fixed set of URLs, the wpseo_sitemap_{$type}_content filter lets WordPress code append XML to a Yoast-generated sitemap. The example below uses wpseo_sitemap_page_content, so it adds entries to the page sitemap:

add_filter( 'wpseo_sitemap_page_content', 'my_site_add_custom_sitemap_urls' );

function my_site_add_custom_sitemap_urls( $content ) {
    $urls = [
        'https://www.example.com/special-page/',
        'https://www.example.com/another-special-page/',
    ];

    foreach ( $urls as $url ) {
        $content .= "\n\t\n";
        $content .= "\t\t" . esc_url( $url ) . "\n";
        $content .= "\t\n";
    }

    return $content;
}

Replace the example URLs with the pages’ preferred canonical URLs: the final URLs you want search engines to treat as the main versions. Avoid URLs that redirect or include tracking parameters. WordPress calls the callback function with Yoast’s existing sitemap content. The function adds <url> and <loc> elements, then returns the complete content.

The filter name selects the sitemap type. Use the page hook only for page-sitemap entries; check the sitemap index to identify the correct type for other content. Yoast’s current sitemap renderer code applies this content filter only to the first page of a sitemap. That makes the filter a poor fit for a large or frequently changing URL collection. Yoast documents a default limit of 1,000 entries per sitemap, so adding many custom entries to the first page also complicates sitemap pagination.

Return valid XML. Add a <lastmod> value only if your code has a reliable modification date. Place the code in a small site-specific plugin or a child theme so a parent-theme update does not erase it. Test the change on a staging site before using it on the live site.

Use WordPress content for a growing URL set

When the URLs represent pages that you manage in WordPress, store them as WordPress content rather than maintaining a hard-coded list. A public custom post type can work well for a distinct content collection. Yoast can then generate sitemap entries from those records, while WordPress stores their URLs, publication status, and updates.

Enable the content type for search results in Yoast and make sure the individual records are not set to noindex. Read how Yoast handles custom post type sitemaps for details. If another service owns the URLs and generates its own sitemap, keep that collection separate instead of treating its entries as WordPress content.

Add another sitemap to Yoast’s index

If an external service already produces a sitemap file, add its URL to Yoast’s sitemap index so search engines can follow the index entry to that sitemap. The Yoast API uses wpseo_sitemap_index to add the reference. It does not copy the external sitemap’s individual URL entries into Yoast’s page sitemap.

Make sure the referenced sitemap exists and is reachable. Yoast’s external sitemap instructions also warn that a hard-coded lastmod date in the example does not update automatically. Omit that date unless your code can keep it accurate.

Check the sitemap after the change

Open Yoast’s sitemap index and follow the link to the relevant sitemap. Confirm that each intended URL appears once, the XML loads without errors, and each URL leads to the intended page rather than a redirect or error page.

If the sitemap still shows old content, clear the caches that apply to the site. These may include plugin caches, WordPress caches, and server caches. Yoast lists caching among the causes of sitemaps that do not update. After the sitemap reflects the change, remember that listing a URL helps search engines discover it but does not guarantee indexing.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *