DEV Community

Дарья
Дарья

Posted on

I Built a PHP Sitemap That Fixed My Google Indexation Problem

My WordPress site has 65 posts, but Rank Math's sitemap was only showing 49. The remaining 16 posts were invisible to Google. Here's how I fixed it with a 30-line PHP file.

The Problem

Rank Math Free limits post-sitemap.xml to a fixed number of entries. When I published post #50, it silently pushed post #1 out of the sitemap. Google eventually deindexed the missing pages.

The Fix: ww-sitemap.php

I created a PHP file in my WordPress root that generates a sitemap dynamically from the database, excluding any URLs already in Rank Math's sitemap (to avoid duplicates):

<?php
header('Content-Type: application/xml; charset=utf-8');
require_once(__DIR__ . '/wp-load.php');

$posts = get_posts(['post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1]);

// Get existing sitemap URLs to avoid duplicates
$xml = @file_get_contents(home_url('/post-sitemap.xml'));
preg_match_all('/<loc>([^<]+)<\/loc>/', $xml, $m);
$existing = array_flip($m[1] ?? []);

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

foreach ($posts as $post) {
    $url = get_permalink($post);
    if (!isset($existing[$url])) {
        $mod = get_the_modified_date('Y-m-d', $post);
        echo "<url><loc>{$url}</loc><lastmod>{$mod}</lastmod></url>";
    }
}
echo '</urlset>';
Enter fullscreen mode Exit fullscreen mode

Then I submitted ww-sitemap.php in Google Search Console alongside the existing sitemap_index.xml.

The Result

  • Before: 49 URLs in sitemap, 16 posts invisible to Google
  • After: All 65 posts discoverable
  • Bing picked up the missing pages within 48 hours via IndexNow

Lessons Learned

  1. Always verify your sitemap contains all your pages — don't assume the plugin handles it
  2. Dynamic PHP sitemaps are a valid fallback when plugin settings aren't accessible
  3. Submit supplementary sitemaps in GSC — you can have multiple

Site: watchwalls.pro — an Apple Watch wallpaper resource where I discovered this problem.

Top comments (0)