XML WordPress sitemap generated by PHP

POSTED IN Tutorials

Below you will find the PHP code for generating a proffesional XML sitemap, compatible with Google Webmaster Tools. The PHP script connects to the WordPress database, from where it gets all the Posts and post them in the XML.

The entire script is fully automatized, you only have to upload it in public_html. Easy ha!? ;)

< ?php require('wp-blog-header.php');
header('Content-type: text/xml; charset=' . get_settings('blog_charset'), true); ?>
< ?php echo ''; ?>


            < ?php bloginfo('url') ?>
            < ?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT'), false); ?>
            always
1.0

        < ?php $sitemap = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' ORDER by post_modified DESC"); ?>
        < ?php foreach ($sitemap as $sitemap) { ?>

            < ?php echo get_permalink($sitemap->ID); ?>
            < ?php echo mysql2date('Y-m-d\TH:i:s\Z', $sitemap->post_modified, false); ?>
            daily
0.8

< ?php } ?>

Leave a Reply

Go to Top