Metro_Sitemap::build_root_sitemap_xml PHP Method

build_root_sitemap_xml() public static method

Build Root sitemap XML - currently all days
public static build_root_sitemap_xml ( )
    public static function build_root_sitemap_xml()
    {
        $xml_prefix = '<?xml version="1.0" encoding="utf-8"?>';
        global $wpdb;
        // Direct query because we just want dates of the sitemap entries and this is much faster than WP_Query
        $sitemaps = $wpdb->get_col($wpdb->prepare("SELECT post_date FROM {$wpdb->posts} WHERE post_type = %s ORDER BY post_date DESC LIMIT 10000", Metro_Sitemap::SITEMAP_CPT));
        $xml = new SimpleXMLElement($xml_prefix . '<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></sitemapindex>');
        foreach ($sitemaps as $sitemap_date) {
            $sitemap_time = strtotime($sitemap_date);
            $sitemap_url = add_query_arg(array('yyyy' => date('Y', $sitemap_time), 'mm' => date('m', $sitemap_time), 'dd' => date('d', $sitemap_time)), home_url('/sitemap.xml'));
            $sitemap = $xml->addChild('sitemap');
            $sitemap->loc = $sitemap_url;
            // manually set the child instead of addChild to prevent "unterminated entity reference" warnings due to encoded ampersands http://stackoverflow.com/a/555039/169478
        }
        return $xml->asXML();
    }