For very large sites, generating sitemaps on page load can exceed PHP execution time and lead to timeout errors. To avoid this, you can generate your XML Sitemaps ahead of time using WP-CLI — bypassing the web-request environment and its limits.
Why Use WP-CLI?
- Avoid Timeouts: CLI processes aren’t bound by the usual PHP max-execution-time limits.
- Automation-Friendly: Easily integrate into deployment scripts or cron jobs.
- On-Demand Control: Generate exactly when you choose, rather than waiting for the first visitor to trigger sitemap generation.
Note: By default, sitemaps are regenerated automatically when someone visits the sitemap URL. Use WP-CLI when you need more reliability for very large sitemaps.
Basic Usage
wp sitemap generate
This command builds the Main XML Sitemap.
Specifying a Sitemap Template
Generate specialized sitemaps by passing the –template flag:
wp sitemap generate --template=image-sitemap
This creates Image Sitemap. Supported Templates are:
- sitemap (Main XML Sitemap) (Default value for template)
- image-sitemap (Image Sitemap)
- video-sitemap (Video Sitemap)
- google-news (Google News)
With these simple WP-CLI commands, you can reliably generate and refresh your sitemaps — keeping Search Engines up to date without risking PHP timeouts.