Google Sitemaps and WordPress 2.0

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.


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="">';

// 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);
        <loc><?php the_permalink(); ?></loc>
        <lastmod><?php echo get_post_modified_time('Y-m-d') ?></lastmod>

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.