Create a Custom Sitemap with custom lastmod params

#649110
  • Resolved Anonymous
    Rank Math free

    Hey Team,

    Followed this > https://rankmath.com/kb/custom-sitemaps/

    And have it working with my dynamic data which lists out all the indexable URL’s.

    However, I cannot control the lastmod value?

    I have stripped back my code to that of the docs above and still cannot get the last mode dates to propagate in the site map?


    namespace RankMath\Sitemap\Providers;

    defined( 'ABSPATH' ) || exit;

    class Custom implements Provider {

    public function handles_type( $type ) {
    return 'properties' === $type;
    }

    public function get_index_links( $max_entries ) {
    return [
    [
    'loc' => \RankMath\Sitemap\Router::get_base_url( 'properties-sitemap.xml' ),
    'lastmod' => '',
    ]
    ];
    }

    public function get_sitemap_links( $type, $max_entries, $current_page ) {
    $links = [
    [
    'loc' => 'http://www.example.com/',
    'lastmod' => '2023-01-01',
    'images' => [
    [
    'src' => 'http://www.example.com/image1.png',
    'title' => 'Image Title',
    ],
    [
    'src' => 'http://www.example.com/image2.png',
    'title' => 'Image 2 Title',
    ],
    ],
    ],
    [
    'loc' => 'http://www.example.com/2',
    'lastmod' => '2023-09-27 22:00 +00:00' // Testing with current rankmath lastmod string
    ],
    ];

    return $links;
    }
    }

    See image of the site map generated:
    Custom Sitemap

    I have tried this on a fresh install with just Rankmath and still same issue.

    I am clearing the cache so this is not the issue. I have also found that the code below within functions.php can create blocking issues (running other php scripts directly) which results in a critical error when this code is present – no errors in logs or in wp_debug mode


    // Add generated property URLs into Rank Maths site map index
    include_once( get_stylesheet_directory() . '/php/properties-sitemap.php');
    add_filter('rank_math/sitemap/providers', function( $external_providers ) {
    $external_providers['custom'] = new \RankMath\Sitemap\Providers\Custom();
    return $external_providers;
    });

    add_filter( 'rank_math/sitemap/enable_caching', '__return_false');

    Let me know if you need any extra info

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

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for the delay and for any inconvenience this issue may have caused you.

    Regarding the critical issue, please update this line to match your custom sitemap:

    $external_providers['custom'] = new \RankMath\Sitemap\Providers\Custom();

    to

    $external_providers['properties'] = new \RankMath\Sitemap\Providers\Custom();

    Regarding the lastmod, I was able to replicate this on our test install and forwarded it to our development team for investigation.

    Our team will get back to you shortly after troubleshooting.

    We would really appreciate your kind patience regarding this matter.

    Thank you.

    Anonymous
    Rank Math free

    Thanks Reinelle,

    Changing 'custom' to 'properties' (my variable) has fixed the issue with blocking/causing errors with other scripts šŸ™‚

    I will wait for the you/team to get back to me on this

    Cheers
    Alex

    Hello,

    Thank you for your patience.

    We have an update from our development team, and the code to show the lastmod should be 'mod' only.

    This applies to get_sitemap_links(), but for get_index_links(), you’ll use 'lastmod'.

    Here’s the updated code you have shared above to try as well:

    namespace RankMath\Sitemap\Providers;
    
    defined( 'ABSPATH' ) || exit;
    
    class Custom implements Provider {
    
    public function handles_type( $type ) {
    return 'properties' === $type;
    }
    
    public function get_index_links( $max_entries ) {
    return [
    [
    'loc' => \RankMath\Sitemap\Router::get_base_url( 'properties-sitemap.xml' ),
    'lastmod' => '',
    ]
    ];
    }
    
    public function get_sitemap_links( $type, $max_entries, $current_page ) {
    $links = [
    [
    'loc' => 'http://www.example.com/',
    'mod' => '2023-01-01',
    'images' => [
    [
    'src' => 'http://www.example.com/image1.png',
    'title' => 'Image Title',
    ],
    [
    'src' => 'http://www.example.com/image2.png',
    'title' => 'Image 2 Title',
    ],
    ],
    ],
    [
    'loc' => 'http://www.example.com/2',
    'mod' => '2023-09-27 22:00 +00:00' // Testing with current rankmath lastmod string
    ],
    ];
    
    return $links;
    }
    }

    We have also submitted this to our documentation team to update our knowledgebase link about the custom sitemap and will get it updated soon.

    Hope that helps.

    Anonymous
    Rank Math free

    Hi Reinelle,

    Such a simple fix!

    Thanks for getting back to me, all is working as expected now šŸ™‚

    Cheers
    Alex

    Hello,

    We are glad to hear that this issue has been resolved. Thank you for letting us know. This ticket will be closed now, but you can always open a new one if you have any other questions or concerns. We are here to help you with anything related to Rank Math.

    We appreciate your patience and cooperation throughout this process.

    Thank you for choosing Rank Math.

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

The ticket ‘Create a Custom Sitemap with custom lastmod params’ is closed to new replies.