Generating Sitemap External to Wordpress
<p>So, i've built an "external to wordpress" sitemap generator using PDO to query the wordpress database.</p>

<p>I have found (after a month) an issue with it tho that I am completely stumped on.
Parent -> Children links in it.  And of course this is generating 404's when on spidering because officially, the child pages don' exist.</p>

<p>The sites are setup with permalinks as <code>/%postname%/</code></p>

<p>And the code is:</p>

<pre><code>&lt;?php

require dirname(__FILE__) . '/wp-config.php';
define('SITE_URL' , 'http://www.theactualdomain.com/'); // Change this for the site to pull this for

header('Content-Type: text/xml');

echo GenerateSiteMap();

function GenerateSiteMap(){
    global $table_prefix;
    $dsn = array('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
    $dbh = new PDO($dsn[0], $dsn[1], $dsn[2]);
    $dbh-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $dbh-&gt;setAttribute(PDO::ATTR_PERSISTENT, true);
    $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
    // Pages
    $sql = "SELECT a.`post_name` As Slug, a.`post_date` As Date FROM `".$table_prefix."posts` a WHERE a.`post_status` = 'publish' AND a.`post_type` = 'page'";
    $stmnt1 = $dbh-&gt;prepare($sql);
    $stmnt1-&gt;execute();
    $rs0 = $stmnt1-&gt;fetchAll(PDO::FETCH_ASSOC);
    // Posts    
    $sql = "SELECT a.`post_name` As Slug, a.`post_date` As Date FROM `".$table_prefix."posts` a WHERE a.`post_status` = 'publish' AND a.`post_type` = 'post'";
    $stmnt1 = $dbh-&gt;prepare($sql);
    $stmnt1-&gt;execute();
    $rs1 = $stmnt1-&gt;fetchAll(PDO::FETCH_ASSOC);
    // Categories
    $sql = "Select b.`slug` As Slug, NOW() As Date From `".$table_prefix."term_taxonomy` a Inner Join `".$table_prefix."terms` b On b.`term_id` = a.`term_id` Where a.`taxonomy` = 'category' AND a.`count` &gt; 0";
    $stmnt1 = $dbh-&gt;prepare($sql);
    $stmnt1-&gt;execute();
    $rs2 = $stmnt1-&gt;fetchAll(PDO::FETCH_ASSOC);
    // Tags
    $sql = "Select b.`slug` As Slug, NOW() As Date From `".$table_prefix."term_taxonomy` a Inner Join `".$table_prefix."terms` b On b.`term_id` = a.`term_id` Where a.`taxonomy` = 'post_tag' AND a.`count` &gt; 0";
    $stmnt1 = $dbh-&gt;prepare($sql);
    $stmnt1-&gt;execute();
    $rs3 = $stmnt1-&gt;fetchAll(PDO::FETCH_ASSOC);
    // Archives
    $sql = "SELECT DISTINCT CONCAT(DATE_FORMAT(`post_date`, '%Y'), '/', DATE_FORMAT(`post_date`, '%m')) As Slug, NOW() As Date FROM `".$table_prefix."posts` WHERE `post_status` = 'publish' AND `post_type` = 'post'";
    $stmnt1 = $dbh-&gt;prepare($sql);
    $stmnt1-&gt;execute();
    $rs4 = $stmnt1-&gt;fetchAll(PDO::FETCH_ASSOC);
    $dbh = null;$dsn = null;$sql = null;$stmnt1 = null;
    unset($dbh, $dsn, $stmnt1, $sql);
    // Home Page and Sitemap Starter
    $ret = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;
                &lt;urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" 
                        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&gt;    
                    &lt;url&gt;
                        &lt;loc&gt;' . SITE_URL . '&lt;/loc&gt;
                        &lt;lastmod&gt;' . date('c',time()) . '&lt;/lastmod&gt;
                        &lt;changefreq&gt;daily&lt;/changefreq&gt;
                        &lt;priority&gt;1.0&lt;/priority&gt;
                    &lt;/url&gt;';
    // Pages
    $ret .= FormatMapping($rs0, 'weekly', '1.0', 4);
    unset($rs0);
    // Posts
    $ret .= FormatMapping($rs1, 'weekly', '0.9', 0);
    unset($rs1);
    // Categories
    $ret .= FormatMapping($rs2, 'daily', '0.8', 1);
    unset($rs2);
    // Tags
    $ret .= FormatMapping($rs3, 'daily', '0.8', 2);
    unset($rs3);
    // Archives
    $ret .= FormatMapping($rs4, 'monthly', '0.7', 3);
    unset($rs4);
    $ret .= '&lt;/urlset&gt;';
    return $ret;
}

function FormatMapping($rs, $cf = 'daily', $priority = '0.5', $type = 0){
    $ret = '';
    $url = '';
    $rCt = count($rs);
    for($i = 0; $i &lt; $rCt; ++$i){
        $ret .= '&lt;url&gt;';
        switch($type){
            case 0: // Posts
            case 3: // Archives
            case 4: // Pages
                $url = SITE_URL . $rs[$i]['Slug'];
                break;  
            case 1: // Categories
                $url = SITE_URL . 'category/' . $rs[$i]['Slug'];
                break;
            case 2: // Tags
                $url = SITE_URL . 'tag/' . $rs[$i]['Slug'];
                break;
        }
        $ret .= '   &lt;loc&gt;' . $url . '&lt;/loc&gt;
                    &lt;lastmod&gt;' . date('c', strtotime($rs[$i]['Date'])) . '&lt;/lastmod&gt;
                    &lt;changefreq&gt;' . $cf . '&lt;/changefreq&gt;
                    &lt;priority&gt;' . $priority . '&lt;/priority&gt;
                &lt;/url&gt;';
    }
    unset($rs);
    return $ret;
}
</code></pre>

<p>How can I set that initial <code>Page</code> query up to render the links properly?</p>

<p>No, using the Wordpress object is not a solution.  The whole point is to lessen the load on already heavy traffic sites, when applying the sitemap.</p>