Google Sitemaps and WordPress 2.0

by
Annika Backstrom
in Meta, on 30 March 2006. It is tagged and #WordPress.

I've created a script to dump my blog post history into an XML file suitable for Google Sitemaps. Note that I consider any XML-building script that does not use XML creation tools to be a hack. This is a hack.

<?php

header("Content-type: application/xml");
require_once( dirname(__FILE__) . '/wp-config.php'); // get the wordpress functions

// sitemap header
echo '<'.'?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';

// set upper limit to a sufficiently high number
$posts = get_posts('numberposts=32767&order=ASC&orderby=post_date');

// output all the posts
foreach($posts as $post): setup_postdata($post);
?>
    <url>
        <loc><?php the_permalink(); ?></loc>
        <lastmod><?php echo get_post_modified_time('Y-m-d') ?></lastmod>
    </url>
<?php

endforeach;
echo '</urlset>';

Example: sitemap.php.

I also add a rewrite condition to my .htaccess file:

RewriteRule ^sitemap.xml$ sitemap.php [L]

Thus is born sitemap.xml.