Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8879149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:53:17+00:00 2026-06-14T19:53:17+00:00

I have a WordPress site with about 300k pages in it now, and a

  • 0

I have a WordPress site with about 300k pages in it now, and a server with 1GB of memory. Unfortunately, all of the sitemap generating plugins can no longer handle it. I’ve tried 3 different methods of writing to XML with PHP (XMLWriter, SimpleXMLElement, and DOMDocument) and they all end up capping out at around 30k pages (xml nodes).

What do you think I could do to make this work? Worst case scenario, I thought about setting up multiple cron jobs that would run every ten minutes once a day and keep opening/appending to the file and just add to it in chunks, but that’s obviously not an optimal solution. I found some snippet that claimed to be able to flush out the memory during my loop, but it didn’t do the trick either. Here’s an example of that snippet:

$xml = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 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">';
for ($i = 0 ; $i< 20; $i++) {
  $query = mysql_query(sprintf("SELECT ID, post_date FROM wp_posts WHERE post_status='publish' LIMIT %s,%s", $i*10000, 10000));
  while ($row = mysql_fetch_array($query)) {
    $xml .= '<url>';
    $xml .= '<loc>'.get_permalink($row['ID']).'</loc>';
    $xml .= '<lastmod>'.$row['post_date'].'</lastmod>';
    $xml .= '<changefreq>weekly</changefreq>';
    $xml .= '<priority>0.6</priority>';
    $xml .= '</url>';
  }
}
$xml .= '</urlset>';

$sxe = new SimpleXMLElement($xml);
$sxe->asXML("sitemap.xml");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:53:18+00:00Added an answer on June 14, 2026 at 7:53 pm

    Why are you grabbing all the records at once?

    Try get 10000 rows per request. And clean up memory after every iteration.

    If you run in cli mode older versions of php do not release memory so you can try to fork http://php.net/manual/en/function.pcntl-fork.php

    How to do it:

    1. No need to use any xml library, sprintf would do the trick.
    2. Wrap it in to for ($i = 0, $i < 5, $i++) {}
    3. Query would look like LIMIT ($i*10000) 10000

    Code Example:

    for ($i = 0 ; $i< 5 $i++) {
        ...
        $sth = $dbh->prepare('SELECT * FROM table_name LIMIT ? ?');
        $sth->execute(array($i*10000, 10000));
        ...
    }
    

    Another code example:

        <?php
        $fileHandle = fopen("sitemap.xml", "w");
    
        fwrite($fileHandle,
            '<?xml version="1.0" encoding="UTF-8"?>' . 
            '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' .
            ' 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">'
        );
    
        for ($i = 0 ; $i< 20; $i++) {
            $query = mysql_query(sprintf("SELECT ID, post_date FROM wp_posts WHERE post_status='publish' LIMIT %s,%s", $i*10000, 10000));
            $xml = '';
            while ($row = mysql_fetch_array($query)) {
                $xml .= '<url>'.
                    '<loc>'.get_permalink($row['ID']).'</loc>' .
                    '<lastmod>'.$row['post_date'].'</lastmod>' .
                    '<changefreq>weekly</changefreq>' .
                    '<priority>0.6</priority>' .
                    '</url>';
            }
    
            fwrite($fileHandle, $xml);
        }
        fwrite($fileHandle, '</urlset>');
    
        fclose($fileHandle);
    
        echo PHP_EOL . 'memory used: '. memory_get_peak_usage() . PHP_EOL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three pages in my WordPress site: About, Voices & History. These are
I have a WordPress site which have 4 pages with one page is a
I have a wordpress site that i've been working on that has some pages
I have some jQuery powered widgets on my WordPress site. They all work fine
I have a wordpress site, it's already have about about 3000 published posts, the
Just about to launch a WordPress site but have noticed that it's currently loading
I've been asked to recreate an HTML site in WordPress and have it all
Currently I have a wordpress site on the local development server that has a
I've been working on this wordpress site for about a week now, and today
I have just installed a WordPress site on GoDaddy but unfortunately the home page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.