Google Sitemaps with WordPress

I spent a little time and worked up some php code that generates a Google Sitemap from a WordPress site. The php code generates a Google Sitemap xml file with all your posts ordered by time – newest posts first. Here’s a link to my Google Sitemap so you can see how it looks.

I’m sure there are ways to improve on my code so go right ahead. If you do, just leave me a comment. I might want to download it from you.

Just copy this code into a php file.

You can refer back to my previous post to read more about the Google Sitemaps Protocol and how you can submit your Google Sitemap.

Update: Wanted to point out that this generates a Google Sitemap from all your posts. There is no page support or category support. It is real basic and can definitely be improved.

Update #2: I won’t be writing a plugin for this since Arne Brachhold has already written a great one. Thanks Arne.

107 thoughts on “Google Sitemaps with WordPress

  1. Quick and nice start to help the community use this new Google feature, thanks for it.

    I have a couple suggestions that are easy to implement :

    use gzencode to produce a lighter .tgz instead of raw xml. Since Google accepts gzipp’ed documents, let’s not waste our bandwidth

    modify your SQL query : not only “post_status = ‘publish’” but also “post_status = ‘static’” to indexes pages as well as posts

    you could probably fine tune the priority rather easily : for example checking post_modified, and giving a different priority depending on how old the post is.
    This said, I (we ?) have no clue of what Google will do with this priority …

    You could add a “loc” entry for categories as well. I find my category pages (/category/linux/ etc..) are quite often accessed from Google searches, so I think it’s important they’re well crawled and indexed as well.

    I also have a few ideas of further improvements that will be quickly needed :)

    You could easily make your script a plugin that would be run when a post is either created or modified.

    The thing I’d suggest here is that the sitemap is updated when a post is modified, and that it is updated and submitted to Google when a post is created (with maybe a maximum number of daily submission, although I couldn’t find any guidelines about this on Google’s pages)

    This would make another feature quite mandatory : it’d be a waste of computer time to generate the whole sitemap each time a post is created, especially on large blogs with numerous posts. A workaround would be to make incremental updates.

    It could be done by dynamically generate entry for latest post and merge it with a static file containing all previous posts.

    So. This were my first thoughts about your work. Again, thank you for being quick on this.

  2. Pingback: planetOzh

  3. Many thanks for that – however, just a quick question – is this going to be difficult to work with presenting an individual feed for individual category groups, or will it only work with generating a feed for the entire WP site?

  4. Pingback: Search Engine Roundtable

  5. Pingback: ????????

  6. Pingback: Microsiervos

  7. Pingback: Lockergnome's Internet Marketing

  8. Pingback: Un Passo Al Giorno

  9. Pingback: jarkolicious

  10. Pingback:

  11. Hey there! Thanks for the idea, I spent a few minutes working it into my own CMS.

    Re: categories & sitemap sitemap; I have some down time so if I come up with
    something psuedocodey I will comment again. :)

    One little question, as I am not very familiar with the inner workings of WordPress: Are all dates UTC/Zulu?

    Thanks man!

  12. Pingback: WebMaster View

  13. With regard to the suggestion to have the script submit new ones every so often, the guidelines for Google’s own generator say a maximum of once per hour.

  14. Pingback:

  15. Thanks for the great script guys. I’ve managed to get my sitemap up and running but Google doesn’t like the code somehow.

    When submitted, Google returns the error ‘We couldn’t find the Sitemap at the location you provided. Please make sure the Sitemap URL is correct and resubmit your Sitemap.’

    Anyone else come across this yet?

  16. Pingback: I VISION

  17. Pingback: juque § Weblog

  18. Hello

    I think you need to use utf8_encode to be on the safe side…

    echo utf8_encode(”); ?>


    get_results(“SELECT * FROM $wpdb->posts WHERE post_status = ‘publish’ ORDER by post_modified DESC”); ?>

    ID)); ?>
    post_modified, false)); ?>

  19. Ozh said, “The thing I’d suggest here is that the sitemap is updated when a post is modified [...] it’d be a waste of computer time to generate the whole sitemap each time a post is created, especially on large blogs with numerous posts. A workaround would be to make incremental updates.”

    Except you can have multiple sitemaps. So, have a sitemap for everything prior to the current month and a second for things added this month. Only the second needs to be recreated and shipped to Google every hour.

  20. So, finally… I tried to comment (and trackback) already yesterday with a new script that includes static pages (as Ozh suggested) and also dt’s gzip-compression.
    Best thing on this script is the frequency the sitemap is generated. When requested, it is checked whether the last backup is more recent than the last post modification. If necessary, a new backup is generated and eventually, this backup is returned. I’ll see to it to include the new ideas, too.

    Get the script at

    Oh, and it seems to work with Google, I submitted the map yesterday. Still have to see if they complain now.

  21. I merged your script with the one by Arne Brachhold, so that the final script also has category pages and can be configured. Also it generates the sitemap only if necessary (it does a backup to a certain file, and if nothing has changed, only the backup is returned.). And I included the idea by dt for gzip.
    Hope, you’re okay with this.

    Download at


  22. Thanks …
    I submitted sitemap of my blog thanks to your script … but I get Parsing error. why?


  23. Some competition for Arne ;-) : I converted my original remix into a real plugin for WordPress. Includes real timezones and saves you some time by only generating if really necessary. Download at my site.


  24. Sorry to spam so much (delete the old posts by me, if you like), but I just wanted to let you know: I added a feature to my plugin that informs Google every time you publish, edit or delete a post. Never again having to submit your sitemap manually.
    Also, most (all?) features of Arne Brachhold’s plugin are in it, and real timezones and a min priority for posts (it’s no good if a post has a priority of “0.0″).


  25. Pingback: Home of Arne Brachhold

  26. Pingback: # k u k i e » WordPress ?????

  27. Pingback: » google sitemap

  28. Pingback: juque § weblog

  29. Pingback: » Mr. Ploppy’s Monday Tool List - Volume XXIV - Stuntdubl - SEO Consultant

  30. Pingback: OddThinking » Judging the initial WordPress Google Sitemap Plugins

  31. Pingback: OddThinking » Judging the WordPress Google Sitemap Plugins - Redux

  32. Pingback: ? ? ? ? » Blog Archive » WordPress?sitemap??

  33. Pingback: I VISION » Google Sitemaps help indexing

  34. Pingback: kukie » WordPress ?????

  35. Pingback: Php???????? » Google Sitemap Plug-in for wordpress

  36. Pingback: Php???????? » Google Sitemap Plug-in for wordpress

  37. Pingback: Working Blogger»Blog Archive » Google Sitemaps for Bloggers

  38. Pingback: Home of Arne Brachhold » Google Sitemap Generator for WordPress 2.5

  39. Pingback: Frank Beier

  40. Thanks for providing this plugin, until recently I was using it for my sitemap. After spending some time revitalizing my website I came back here to find the link to the WordPress plug-in. I have no migrated, but I appreciate the work you did.

  41. Pingback: Google Sitemaps to Improve Indexing » Search Engine Optimization » SEO For Wordpress

  42. I am using the Plugin and it works great from Day one.

    If your site is not verified than you missed making the dummy Google File and uploading it to your root folder on your server. Once you have the file up there click on the “verify” button.

    All that verify means is that the dummy file has not been found or checked yet.

    Follow the instructions given by Google on that point and all will be well.

  43. Pingback: Addio monti. . . | Central Scrutinizer - [ il web in 272 comode rate ]

  44. Pingback: aoiro-blog » Google Sitemaps (BETA)?WordPress

  45. Pingback: Bagak.Com

  46. Pingback: BLog | kaNg aSep » Breaking Down Google Sitemaps XML

  47. Pingback: » Weblog for Ramblings from the shupe family.

  48. Pingback: blog.nas » Generate Google Sitemap for WordPress

  49. Pingback: Wordpress30?????? | BiZwiKi.CN - ?? PK ??

  50. Pingback: 23rd World » Google’s Sitemap Buzz

  51. Pingback: Blog » Blog Archive » Free Online - Web - Hosting

  52. Pingback: Google Site Maps at web –> blog

  53. Hello!

    Very good work, simple and eficient code! Thanks a lot. I have noticed an “error”, if you publish some post to be appeared in the future (set future time to them, useful if you are going to be inactive in a comming period…), this script does not respect it and show them.

    Another plugin I have showed, as this one has the same problem.

    I have updated the query to:

    $t = time ();
    $today = date (“Y-m-d H:i:s”,$t);
    $sitemap = $wpdb->get_results(“SELECT * FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_date

  54. Pingback: Google Sitemaps (BETA) « :: plasticdreams ::

  55. Pingback: BoGoMo » Blog Archive » [?]????? ?? ? ? ?? ????

  56. Pingback: 23rd World » Google Sitemaps Explained

  57. Pingback: ¸µ¥Á¥ã¥Ã¥È¥ì¥Ç¥£¡ª¥¬¥Ã¥Ä¥ê·î¼ý100Ëü½÷µ¯¶È²È¤Ø¤ÎÆ»

  58. Thanks for all the hard work on creating this. I plan to put up a WordPress blog soon and have a lot to learn about the whole process.

    You have done a lot of work to help a lot of people here.


  59. Pingback: WORDPRESSNEWS.COM » WordPress - - Translate this page

  60. Pingback: JulyBox–七月盒子 » Google Sitemap 生成工具 for wordpress

  61. Pingback: Levysoft » Google rilascia xml sitemap protocol

  62. Pingback: Blogger Buster

  63. Great post and very useful information.This plugin is certainly going to help a lot of WordPress bloggers get quicker search engine optimization.
    Thanks very much for the info.

  64. Pingback: Webmasterdays » Blog Archive » Google Sitemaps with Wordpress - Social Patterns

Comments are closed.