WordPress Sitemaps for Multisite Setups
I run several WordPress blogs with one codebase. This allows me to update the codebase once, for all by blogs and also allows me to share plugins. It’s convenient and the way to go if you want to focus and tailor your blogs by topic or audience.
One detractor from this setup is trying to get sitemaps generated for each blog. At the time of posting, current plugins which generate sitemaps were not able to successfully handle this type of multiblog, one code base setup. This is annoying at minimum and possibly damning depending on how well the bots can crawl my page.
My first thought was to go ahead and modify the plugin. How hard could it be? Answer, too hard. I’m sure it can be done, but I have better things to do then swim in that code and figure it out. Next thought, write my own simple plugin. After all, how hard could it be? Answer, too hard. I’d rather figure out the WordPress API later and need sitemaps generated now.
So, I was left to my own crude devices. What I created were 31 lines of simple bliss which gets the job done. The code hinges on two facts: 1) Google accepts simple text files with URLs as a valid sitemap and 2) it’s easier to locate at the tables WordPress employs and figure them out with a few queries then learn an API/workflow or decipher a bunch of code.
Now, detractors will say that it is better to follow the API and build a plugin. That way the code will continue to work as WordPress evolves. And they are correct. That would be the way to go. But hey, this code was quick to write and will tie me over until something better comes along.
Now for the code, written as a php file
/* Set base url for blog */ define('BASE_URL', 'http://merit.oryo.us/b'); /* Set table name for posts, could be as simple as wp_posts. */ define('TABLE_NAME', 'merit_wp_posts'); /* Set relative paths here to load db credentials */ require("wp-load.php"); require("../../../wordpress/wp-config.php"); $link = mysql_connect('localhost', DB_USER, DB_PASSWORD); if(!$link) { die("Couldn't connect: " . mysql_error()); } mysql_select_db(DB_NAME, $link) || die( mysql_error()); $result = mysql_query("SELECT ID,post_name,post_status FROM " . TABLE_NAME . " ORDER BY post_modified DESC" ); if(!$result) { die("Error: " . mysql_error()); } while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { if($row['post_status'] == "publish") { /* Tweak line to display proper url, BASE_URL . "/index.php?id=" . $row['ID'] . "\n" will surely work */ print BASE_URL . "/" . $row['ID'] . "/" . $row['post_name'] . "\n"; } } mysql_free_result($result); mysql_close($link);
As you can see, there’s not much too it. Simple and unsophisticated but works. Of course, to use this you need to have some understanding of what you are doing. If you can’t make since of the code, best to wait until a sitemap plugin comes along that will properly handle a WordPress site with multiple blogs.


