How to Build a Google News Sitemap in PHP
A Google News sitemap is an XML feed of recently published news
articles. It gives Google a consistent place to find each story's URL,
publication name, language, original publication date, and headline. You
can add news tags to your main sitemap or keep them in a dedicated
/news-sitemap.xml. A separate file is easier to monitor and
can leave the main sitemap free to cover your archive, section pages,
and guides. Google's
news sitemap documentation
Decide which articles belong in the feed
Use the first publication timestamp, not the last edit time or the
time PHP generated the feed. Include only articles created in the last
two days. Old stories can stay live and remain in your regular sitemap
after they leave the news feed. Google allows one news:news
entry per URL and up to 1,000 per file; divide a larger feed across
child sitemaps and list them in a sitemap index. If there are no recent
articles, an intentionally empty news sitemap is okay.
Build the list from public, published news rows with unique, absolute
canonical URLs. Read the visible article headline from the CMS, without
extra bylines, dates, or a site-name suffix. Use the publication name
Google News displays, excluding parenthetical text, and the appropriate
language code, such as en for English or tr
for Turkish. Use an explicit time zone so a late-night story doesn't
move to the wrong date when the server is set to UTC.
Generate XML safely in PHP
This standalone example uses one synthetic article. Save it as
news-sitemap.php. In production, replace the sample values
with the canonical URL, headline, and original publication timestamp
from the CMS. Select and sort eligible rows before rendering, and apply
the page limit to each child sitemap when splitting a large feed. Keep
the publication name and language in site configuration.
<?php
header('Content-Type: application/xml; charset=UTF-8');
function xmlText(string $text): string {
return htmlspecialchars($text, ENT_QUOTES | ENT_XML1 | ENT_SUBSTITUTE | ENT_DISALLOWED, 'UTF-8');
}
// Demo only: in production, use the stored first-publication date.
$published = (new DateTimeImmutable('now', new DateTimeZone('UTC')))->modify('-1 hour');
$url = xmlText('https://example.com/news/market-reopens');
$name = xmlText('Example City News');
$title = xmlText('Market reopens & traders return');
$date = xmlText($published->format(DATE_ATOM));
echo <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
<url>
<loc>{$url}</loc>
<news:news>
<news:publication>
<news:name>{$name}</news:name>
<news:language>en</news:language>
</news:publication>
<news:publication_date>{$date}</news:publication_date>
<news:title>{$title}</news:title>
</news:news>
</url>
</urlset>
XML;
Store headline text unescaped in the CMS and escape it once when
rendering. The demo should emit
Market reopens & traders return, which an XML
parser reads back as the original headline. Don't print PHP warnings or
an HTML layout before the declaration. PHP
XML-compatible escaping
Test the feed before submitting it
With PHP CLI and xmllint installed, run:
php news-sitemap.php > news-sitemap.xml
xmllint --noout news-sitemap.xml
That command checks whether the XML can be parsed. Separately, check
the fields and namespaces, compare <loc> with the
page's canonical URL, and confirm that draft, future-dated, redirected,
and older stories aren't included. Test a headline with an ampersand and
a story just outside the two-day window. Fetch the deployed URL without
signing in and confirm it returns HTTP 200 and XML rather than an error
page. If you cache it, ensure newly published articles appear promptly
and older ones drop out on time.
Serve the feed at a stable URL and submit it through the Sitemaps report for the correct Search Console property. Check the report for fetch or format errors. Submission helps Google discover URLs; it doesn't guarantee indexing or placement in Google News. Google's guide to submitting sitemaps