Documentation Sitemaps – News and XML

Sitemaps – News and XML

Overview #

All public WordPress.com sites come with XML and News sitemaps built-in. The sitemaps are automatically generated, cached for a 24-hour period, and updated whenever a post is published, updated, or deleted (assuming the site is public; sitemaps aren’t produced if your site is still set to private).

You can learn more about the sitemaps here: http://en.support.wordpress.com/sitemaps/

You can customize some of the output by hooking into various filters. A non-exhaustive list of examples is below. If you’re interested in modifying something beyond what’s shown below, just let us know.

The sitemap code is auto-loaded for local testing. You can find it in plugins/vip-do-not-include-on-wpcom/wpcom-plugins/wpcom-sitemap.php.

↑ Top ↑

Comprehensive Sitemaps #

The built-in sitemaps only include a sampling of the data from each site. If you’d like all your content indexed in sitemaps, check out the Comprehensive Sitemaps plugin.

↑ Top ↑

WP.com XML Sitemap: Exclude certain posts #

add_filter( 'sitemap_skip_post', 'x_sitemap_skip_post', 10, 2 );  // Regular sitemap
add_filter( 'wpcom_sitemap_news_skip_post', 'x_sitemap_skip_post', 10, 2 );  // News sitemap

function x_sitemap_skip_post( $skip, $post ) { // $post is an object with properties: ID, post_type, post_modified_gmt, comment_count
	if ( get_post_meta( $post->ID, 'x_skip_post', true ) )
		$skip = true;
	return $skip;
}

↑ Top ↑

WP.com XML Sitemap: Include additional post types #

By default, sitemaps only include posts. Use the following to include additional post types.

add_filter( 'wpcom_sitemap_post_types', 'x_sitemap_add_gallery_post_type' );

function x_sitemap_add_gallery_post_type( $post_types ) {
	$post_types[] = 'gallery';
	return $post_types;
}

↑ Top ↑

WP.com News Sitemap: Include additional post types #

By default, news sitemaps only include posts. Use the following to include additional post types.

add_filter( 'wpcom_sitemap_news_sitemap_post_types', 'x_sitemap_add_gallery_post_type' );

function x_sitemap_add_gallery_post_type( $post_types ) {
	$post_types[] = 'gallery';
	return $post_types;
}

↑ Top ↑

WP.com News Sitemap: Change the publication name #

add_filter( 'wpcom_sitemap_news_sitemap_item', 'x_filter_news_sitemap_name', 10, 2 );

function x_filter_news_sitemap_name( $item, $post ) {
	$item['news:news']['news:publication']['news:name'] = 'My Publication'; // modify as needed for your site
	return $item;
}

↑ Top ↑

WP.com XML Sitemap: Modify the changefreq of an item #

Creates a sliding changefreq for posts, based on their last modified time.

function x_wpcom_sitemap_url( $url, $post_id ) {
	$yesterday 	= strtotime( '-1 day' );
	$last_week 	= strtotime( '-7 days' );
	$last_mod 	= strtotime( $url['lastmod'] );

	if( $last_mod > $yesterday ){
		$url['changefreq'] = 'hourly';
	} elseif( $last_mod > $last_week ) {
		$url['changefreq'] = 'daily';
	} else {
		$url['changefreq'] = 'monthly';
	}	

    return $url;
}

add_filter( 'sitemap_url', 'x_wpcom_sitemap_url', 10, 2 );