Pre 4th of July dinner: ribs, chili, baked beans, watermelon....mmmm

Creating a Dynamic Google Sitemap

My site has a Wordpress blog on it, but it does not power the whole site, just the blog. If my entire site were powered by Wordpress, then I could generate a sitemap for Google using Wordpress functions.

I did not want to have Wordpress control everything; I wanted to have more control. By doing some quick queries, you can generate a sitemap for Google that has all of your blog entries in it.

Setting it Up

First, you will need to send the correct headers, create the opening xml tag, and connect to your database.

<?php
header("Content-Type: text/xml;charset=iso-8859-1");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
require_once('DB CONNECTION GOES HERE'); //This is where I would require my DB connection file

If you are not sure how to connect to your database, you will need to contact your host to get that information, and then do a search for php MySQL connection. If you can’t figure it out still, let me know, and I can help.

Get the Categories

Next, we want to query the database to get the categories used in Wordpress:

$query = "SELECT cat_ID, category_nicename
		FROM wp_categories
		ORDER BY category_nicename";
$result = @mysql_query($query);

Then, we need to loop through the categories and display a url entry for each category:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	echo '<url>
		<loc>http://trevordavis.net/blog/' . $row['category_nicename'] . '/</loc>
		<changefreq>weekly</changefreq>
	     </url>';

Get the Entries

Next, we need to create a query to return all of the entries for each category:

$artQuery = "SELECT p.post_name, DATE_FORMAT(p.post_date, '%Y-%m-%d') AS createdOn
		FROM wp_posts AS p, wp_categories AS cat, wp_post2cat AS pc
		WHERE p.ID = pc.post_id AND pc.category_id = " . $row['cat_ID'] . "
		GROUP BY p.ID
		ORDER BY p.ID DESC";
$artResult = @mysql_query($artQuery);

Finally, we want to create a url entry for each blog entry:

while($artRow = mysql_fetch_array($artResult, MYSQL_ASSOC)) {
	echo '<url>
		<loc>http://trevordavis.net/blog/' . $row['category_nicename'] . '/'. $artRow['post_name'] . '.php</loc>
		<lastmod>'.$artRow['createdOn'].'</lastmod>
		<changefreq>weekly</changefreq>
	     </url>';
}

To finish it off, we just close everything up:

}
echo'</urlset>';?>

The Whole Script

Here is the finished script:

<?php
header("Content-Type: text/xml;charset=iso-8859-1");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
require_once('DB CONNECTION GOES HERE'); //This is where I would require my DB connection file

$query = "SELECT cat_ID, category_nicename
		FROM wp_categories
		ORDER BY category_nicename";
$result = @mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

	echo '<url>
		<loc>http://trevordavis.net/blog/' . $row['category_nicename'] . '/</loc>
		<changefreq>weekly</changefreq>
	     </url>';

	$artQuery = "SELECT p.post_name, DATE_FORMAT(p.post_date, '%Y-%m-%d') AS createdOn
			FROM wp_posts AS p, wp_categories AS cat, wp_post2cat AS pc
			WHERE p.ID = pc.post_id AND pc.category_id = " . $row['cat_ID'] . "
			GROUP BY p.ID
			ORDER BY p.ID DESC";

	$artResult = @mysql_query($artQuery);

	while($artRow = mysql_fetch_array($artResult, MYSQL_ASSOC)) {

		echo '<url>
			<loc>http://trevordavis.net/blog/' . $row['category_nicename'] . '/'. $artRow['post_name'] . '.php</loc>
			<lastmod>'.$artRow['createdOn'].'</lastmod>
			<changefreq>weekly</changefreq>
		      </url>';
	}
}
echo'</urlset>';
?>

Modifying the .htaccess file

We also want to rewrite the url for this file so that it is available at http://trevordavis.net/sitemap.xml. Open up your .htaccess file, or create one if you don’t have one. Then add the following:

RewriteEngine on
RewriteRule sitemap.xml googleSitemap.php

Upload this file along with the googleSitemap.php script to your site root, and you are set. You can also add the rest of your site’s file structure to the sitemap as well. You can see how I did this in my google sitemap.

Sitemaps Protocal

You can read more about the sitemaps protocal to see what other attributes you can apply to each url entry.

By the Way…

Let me know if you can think of any way to improve this.

Share This:
  • del.icio.us
  • Digg
  • Twitter
  • Facebook
  • StumbleUpon
  • Google Bookmarks
  • NewsVine
  • Technorati
  • Reddit
  • Design Float
  • LinkedIn

RSS feed of comments for this entry

  1. Looks nice this will be a big help on my site… really hard making a sitemap…

    tnx

  2. Greetings from the UK!

    I was interested in your xml sitemap article and particularly its ability to generate the blog posts. How would you include the non-dynamic site pages in this sitemap or do you have this as separate. Also, why have the redirect - forgive me if i’m being dumb!!!

    Vinny

  3. @Vinny-
    You could include the static pages right after you open the <urlset> or before you close it.

    I was only doing the redirect so that it would live at /sitemap.xml instead of /googleSitemap.php

Speak Your Mind

* Denotes Required Field

  1. Sick of filling out this form? Register or Log in now.
  2. To post code snippets, use <pre><code>YOUR CODE HERE</code></pre>
Me, Trevor Davis. My blue steel face…

Hi, I’m Trevor Davis

I’m a 25 year old Front-End Developer living in Alexandria, VA. I work full-time at Matrix Group International and freelance on the side.

I specialize in CSS, HTML, jQuery & WordPress. See more of my work, and then hire me.

Recent Work

  • Monica Davis
  • The National Christmas Tree
  • Matrix Group International
  • The MatriX Files
  • Wireless Career
  • George Washington Wired
  • Direct Selling 411
  • Makeup Bizz
  • InstallNET
  • National Park Foundation
  • Worldwide Breast Cancer
  • Food Marketing Institute
See More of My Work

Asides