Add home page to sitemap using code

#1008706
  • Resolved Gus
    Rank Math free

    Hello,

    I’ve noticed that the homepage of my site isn’t appearing in the sitemaps generated by Rank Math. Could you please guide me on how to manually include the homepage URL—whether by modifying my theme’s functions.php file or using an alternative method (like a code snippet or plugin)?

    Any step-by-step instructions or best practices you can offer would be greatly appreciated.

    Thank you!

    Best regards,

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello,

    We apologize for the issue you are facing with your homepage not appearing in the sitemaps generated by Rank Math. To manually include your homepage URL in the sitemap, you can follow these steps:

    Step 1: Create a Custom Sitemap File

    1. Navigate to WordPress Dashboard → Appearance → Theme File Editor (for Classic Theme) or WordPress Dashboard → Tools → Theme File Editor (for Block Theme).
      Theme File Editor

    2. Create a new file in your child theme folder and name it custom-sitemap.php.

    Step 2: Add Code to Custom Sitemap File

    1. Add the following code to the custom-sitemap.php file:

    Sources:
    How to Create a Custom Sitemap and Include It in Rank Math » Rank Math

    Hello,

    Your homepage should be automatically added to your sitemap by default, unless it is getting excluded by one of these reasons:
    https://rankmath.com/kb/url-not-in-sitemap/

    However, here’s a filter you can use and customize to manually add a URL to the sitemap:

    /**
     Filter to add extra URLs to the XML sitemap by type.
     *
     Only runs for the first page, not on all.
     *
     @param string $content String content to add, defaults to empty.
     */
    add_action( 'rank_math/sitemap/page_content', function() {
    return '<url>
    <loc>https://www.yourwebsite.com/</loc>
    <lastmod>2025-04-04T20:20:20+00:00</lastmod>
    </url>';
    });

    And here’s how you can add a filter/hook to your WordPress site:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Once done, please follow these steps to flush your sitemap cache:
    https://i.rankmath.com/pipRDp

    Looking forward to helping you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)

The ticket ‘Add home page to sitemap using code’ is closed to new replies.