Generating latest sitemap of your Wordpress blog for Google Webmaster without plugins
Aug 12, 2012
Suresh Vasudev
3 minute read

I was depending on a Wordpress plugin to generate the sitemap of my blog. However I noticed that it is not getting updated automatically. I searched the forums and I could see many people posting this issue. Some of the popular issues are new posts are not getting reflected in sitemap, pages are not shown in sitemap etc.

I could see various solutions like Wordpress plugins, using crons to re-submit the sitemap every time etc.

My suggestion is to create a small PHP page which can render the sitemap dynamically. The good side of wordpress is it stores all the post and page meta data information in MySQL tables.

A sample sitemap file looks as follows

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="" 

After a quick scan of the Wordpress database tables, I was able to define a query which can return all the published post and pages with the last modified timestamp. This following query will fetch posts and pages in the reverse chronological order.

select post_name,date_format(post_modified,'%Y-%m-%dT%H:%i:%S+00:00') from wp_posts where post_status='publish' and post_type in ('post', 'page') order by id desc

Next step was to create a small php page “sitemap.php” which will execute the above query and render in the format of a sitemap as follows.

echo('&lt;?xml version="1.0" encoding="UTF-8"?&gt;');
echo('&lt;urlset xmlns:xsi="" xsi:schemaLocation="" xmlns=""&gt;');

$con = mysql_connect("{DB SERVER IP}", "{DB USER NAME}", "{DB USER PASSWORD}");
if (!$con)
 die('Could not connect: ' . mysql_error());

mysql_select_db("{WORDPRESS DB NAME}", $con);

$result = mysql_query("select post_name,date_format(post_modified,'%Y-%m-%dT%H:%i:%S+00:00') post_modified from wp_posts where post_status='publish' and post_type in ('post', 'page') order by id desc");

while($row = mysql_fetch_array($result)) {
echo('&lt;url&gt;&lt;loc&gt;&lt;a href="http://www./"&gt;http://www.&lt;/a&gt;{YOUR DOMAIN NAME}/');

The above code will render a sitemap xml containing all the posts and pages published in a wordpress site. Being an example, I have hardcoded the changefreq and priority. We can enhance this to add weightage based on categories , usage of tags associated with the post / page. There exists a lot of scope to enhance the output, but however I am leaving it to your creativity.

Save the above code as sitemap.php (for example) and store it in the home directory of your site.

Test the Sitemap

First make sure that you can access the sitemap from a browser and it is rendering the sitemap as a XML correctly. Use the “view source” to check the XML if it is not getting rendered correctly by your browser.

Next step is to make sure that Google crawler - Google Bot can access this sitemap and it is well formed. The best option is to use the Google Webmaster tools where you can test your sitemap.


Submit the URL of the sitemap and make sure it is well formed and it is acceptable. Once tested you can define it as the permanent sitemap for your wordpress site.


Next time you publish a new post or page , please check the sitemap.php and you could see it reflected in the sitemap. The same sitemap URL can be submitted to BING webmaster tools.

Hurray !!! Freedom from Sitemap Updation!!!