Better late than never, place the following code into your functions.php file (or place in a separate plugin). What this does is get a list of all of the sub-sites, and for each sub-site, get the most recently modified content date (can be post, or page, or whatever), converts it to the proper format, and uses that for the lastmod node of each subsite xml listing.
Tested on WordPress 4.7
// add subsite sitemaps to sitemap.xml
function add_sitemap_custom_items() {
// only add the xml of the subsites to the main site
if (get_current_blog_id() === 1) {
// set blank content variable
$sitemap_custom_items = '';
// set arguments for getting the most recent page (or post, or attachment or post type)
$recent_args = array(
'numberposts' => 1,
'post_type' => 'page', // this can be posts OR attachment, OR whatever post type you want.
'post_status' => 'publish',
'suppress_filters' => true
);
// get the subsites as an object
$subsites = get_sites();
// loop through the subsites
foreach( $subsites as $subsite ) {
// store subsite details
$subsite_id = get_object_vars($subsite)['blog_id'];
$subsite_domain = get_object_vars($subsite)['domain'];
$subsite_slug = get_object_vars($subsite)['path'];
// we don't need to worry about the main site
if ($subsite_id != 1) {
// switch to the site currently in the foreach loop
switch_to_blog($subsite_id);
// get the most recent page (as based on the arguments above)
$subsite_pages = wp_get_recent_posts($recent_args);
// create a loop to get the details of the first post passed
foreach ($subsite_pages as $subsite_page) : setup_postdata($subsite_page);
// get the latest modified timestamp and format it to ISO 8601 date format (aka 'c' in php5)
$latest_mod_date = date('c', strtotime($subsite_page['post_modified']));
endforeach;
// clear query
wp_reset_postdata();
// switch back to the main site
restore_current_blog();
// concatenate this subsite sitemap xml with the domain, slug and proper latest modified content date for that site
$sitemap_custom_items .= "<sitemap><loc>https://".$subsite_domain."".$subsite_slug."page-sitemap.xml</loc><lastmod>".$latest_mod_date."</lastmod></sitemap>\n";
}
}
// output list of sitemap.xml's for each site
return $sitemap_custom_items;
}
}
add_filter( 'wpseo_sitemap_index', 'add_sitemap_custom_items' );
-
This reply was modified 7 years, 11 months ago by
mkormendy.
-
This reply was modified 7 years, 11 months ago by
mkormendy.