Creating a Dynamic Google Sitemap

Posted on July 17, 2007 in Tutorial | 3 Comments »

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:
  • NewsVine
  • Technorati
  • Reddit
  • Google
  • StumbleUpon
  • Facebook
  • Digg
  • del.icio.us
  • Ma.gnolia
  • TwitThis

3 Responses

  1. Ronald AllanSeptember 8, 2007 at 1:51 pm

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

    tnx

  2. vinnyOctober 28, 2008 at 8:55 am

    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. TrevorOctober 28, 2008 at 9:41 am

    @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.

Who Am I?

Trevor Davis I’m Trevor Davis, a 24 year old Front-End Developer. Basically, I make web sites.

By day, I work for Matrix Group International in Alexandria, VA, and by night, I freelance.

Feel free to get in touch with me about anything.

What Have I Done?

  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In

View All My Work »

Bookmarks

  • Google Search Engine Optimization Starter Guide [PDF]

    Google has released a free 22-page Search Engine Optimization Starter Guide containing plenty of well-written, practical and straightforward advice for webmasters. If you've been looking into SEO for a while it probably won't contain anything new for you, but it's useful as a set of guidelines as to what Google considers to be good optimization practice. (psst, Google, with just a little design work it could have looked so much nicer!)

  • The importance of setting expectations

    To make your customer's experience better, be sure to set their expectations.

  • XML Sitemaps Generator

    Insert your URL and let it generate the XML sitemap for you. Very useful for static websites.

  • Train-ee ExpressionEngine Training

    Learn ExpressionEngine with books, screencasts, classroom training and free tutorials from Train-ee.com

  • web.without.words

    Weekly gallery of popular websites reconstructed by removing all words and images, replacing them with blocks.

View All My Bookmarks »