wp_sitemaps_posts_entryfilter-hookWP 5.5.0

Allows you to add additional tags (fields) to individual post sitemap URLs: changefreq, priority, lastmod.

The sitemaps protocol supports four child elements for each sitemap <url> element (WordPress uses <loc> and <lastmod> by default). The <changefreq> and <priority> tags can be added through this filter. Read more in WordPress Sitemap.

Similar filters can add the changefreq, priority, and lastmod tags to taxonomy term and user page links.

Usage

add_filter( 'wp_sitemaps_posts_entry', 'wp_kama_sitemaps_posts_entry_filter', 10, 3 );

/**
 * Function for `wp_sitemaps_posts_entry` filter-hook.
 * 
 * @param array   $sitemap_entry Sitemap entry for the post.
 * @param WP_Post $post          Post object.
 * @param string  $post_type     Name of the post_type.
 *
 * @return array
 */
function wp_kama_sitemaps_posts_entry_filter( $sitemap_entry, $post, $post_type ){
	// filter...
	return $sitemap_entry;
}
$sitemap_entry(array)
An array of the current <url> tag elements in the sitemap.
$post(WP_Post)
The post object for which the sitemap link is created.
$post_type(string)
The post type name. It can be retrieved from the previous parameter as $post->post_type.

Examples

#1 Add the <lastmod> tag to the posts sitemap

add_filter( 'wp_sitemaps_posts_entry', 'kama_sitemaps_posts_entry', 10, 2 );
function kama_sitemaps_posts_entry( $entry, $post ) {
	$entry['lastmod'] = $post->post_modified_gmt;

	return $entry;
}

Before the hook:

...
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<url>
		<loc>http://example.com/template-sticky/</loc>
	</url>
	...

After the hook:

...
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<url>
		<loc>http://example.com/template-sticky/</loc>
		<lastmod>2020-02-28 21:36:01</lastmod>
	</url>
	...

Changelog

Since 5.5.0 Introduced.

Where the hook is called

WP_Sitemaps_Posts::get_url_list()
wp_sitemaps_posts_entry
wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php 164
$sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type );

Where the hook is used in WordPress

Usage not found.