Yoast SEO XML Sitemaps: API documentation
Whilst Yoast SEO provides sensible default behaviors for XML sitemaps (and UI controls for inclusion/exclusion), custom themes or plugins sometimes need to alter our markup or logic. In those cases, you can use the examples below to modify how our sitemaps are generated and output.
Excluding content types
Exclude specific posts
/**
* Excludes posts from XML sitemaps.
*
* @return array The IDs of posts to exclude.
*/
function exclude_posts_from_xml_sitemaps() {
return [ 1, 2, 3 ];
}
add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', 'exclude_posts_from_xml_sitemaps' );
Exclude a post type
/**
* Exclude a post type from XML sitemaps.
*
* @param boolean $excluded Whether the post type is excluded by default.
* @param string $post_type The post type to exclude.
*
* @return bool Whether a given post type should be excluded.
*/
function sitemap_exclude_post_type( $excluded, $post_type ) {
return $post_type === 'recipes';
}
add_filter( 'wpseo_sitemap_exclude_post_type', 'sitemap_exclude_post_type', 10, 2 );
Exclude a taxonomy
/**
* Exclude a taxonomy from XML sitemaps.
*
* @param boolean $excluded Whether the taxonomy is excluded by default.
* @param string $taxonomy The taxonomy to exclude.
*
* @return bool Whether a given taxonomy should be excluded.
*/
function sitemap_exclude_taxonomy( $excluded, $taxonomy ) {
return $taxonomy === 'ingredients';
}
add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );
Exclude an author
/**
* Excludes author with ID of 5 from author sitemaps.
*
* @param array $users Array of User objects to filter through.
*
* @return array The remaining authors.
*/
function sitemap_exclude_authors( $users ) {
return array_filter( $users, function( $user ) {
if ( $user->ID === 5 ) {
return false;
}
return true;
} );
}
add_filter( 'wpseo_sitemap_exclude_author', 'sitemap_exclude_authors' );
Exclude a taxonomy term
/**
* Excludes terms with ID of 3 and 11 from terms sitemaps.
*
* @param array $terms Array of term IDs already excluded.
*
* @return array The terms to exclude.
*/
function sitemap_exclude_terms( $terms ) {
return [ 3, 11 ];
}
add_filter( 'wpseo_exclude_from_sitemap_by_term_ids', 'sitemap_exclude_terms' );
Adding content
Add a custom post type
/**
* Adds an XML sitemap for a custom post type.
*
* @return void
*/
function enable_custom_sitemap() {
global $wpseo_sitemaps;
if ( isset( $wpseo_sitemaps ) && ! empty ( $wpseo_sitemaps ) ) {
$wpseo_sitemaps->register_sitemap( 'recipe', 'create_recipe_sitemap' );
}
}
add_action( 'init', 'enable_custom_sitemap' );
Add additional/external XML sitemaps to the XML sitemap index
/**
* Writes additional/custom XML sitemap strings to the XML sitemap index.
*
* @param string $sitemap_custom_items XML describing one or more custom sitemaps.
*
* @return string The XML sitemap index with the additional XML.
*/
function add_sitemap_custom_items( $sitemap_custom_items ) {
$sitemap_custom_items .= '
<sitemap>
<loc>https://www.example.com/external-sitemap-1.xml</loc>
<lastmod>2017-05-22T23:12:27+00:00</lastmod>
</sitemap>';
return $sitemap_custom_items;
}
add_filter( 'wpseo_sitemap_index', 'add_sitemap_custom_items' );
Add images to posts
Some themes or page builder modules may not show the images on the sitemap. You may need to add them via a filter: wpseo_sitemap_urlimages. This filter will then register images to appear on the sitemap.
/**
* An example of adding images to posts.
*
* @param array $images An array of the images urls related to the post.
* @param int $post_id The post ID.
*
* @return array The array of images urls to add.
*/
function filter_wpseo_sitemap_urlimages( $images, $post_id ) {
array_push( $images, [ 'src' => 'https://www.example.com/wp-content/uploads/extra-image.jpg' ]);
return $images;
};
add_filter( 'wpseo_sitemap_urlimages', 'filter_wpseo_sitemap_urlimages' );
Add images to terms
Some themes, page builder modules or plugins may not show the images on the sitemap. You may need to add them via a filter: wpseo_sitemap_urlimages_term. This filter will then register images to appear on the sitemap.
/**
* An example of adding images to terms.
*
* @param array $images An array of the images urls related to the term.
* @param int $term_id The term ID.
*
* @return array The array of images urls to add.
*/
function filter_wpseo_sitemap_urlimages_term( $images, $term_id ) {
array_push( $images, [ 'src' => 'https://www.example.com/wp-content/uploads/extra-image.jpg' ]);
return $images;
};
add_filter( 'wpseo_sitemap_urlimages_term', 'filter_wpseo_sitemap_urlimages_term' );
Add images to front page
When the front page is not a static page, but the latest posts, you can add images to the sitemap via a filter: wpseo_sitemap_urlimages_front_page. This filter will then register images to appear on the sitemap.
/**
* An example of adding images to front page.
*
* @param array $images An array of the images urls related to the front page.
* @return array The array of images urls to add.
*/
function filter_wpseo_sitemap_urlimages_front_page( $images ) {
array_push( $images, [ 'src' => 'https://www.example.com/wp-content/uploads/extra-image.jpg' ]);
return $images;
};
add_filter( 'wpseo_sitemap_urlimages_front_page', 'filter_wpseo_sitemap_urlimages_front_page' );