Yoast Sitemap - Index all Stores even without products

🧩 Syntax:
add_action( 'init', function() {
    global $WCFMmp, $wpseo_sitemaps;
    remove_action( 'init', [ $WCFMmp->wcfmmp_store_seo, 'register_sitemap' ] );

    if ( is_a( $wpseo_sitemaps, 'WPSEO_Sitemaps' ) ) {
        $wpseo_sitemaps->register_sitemap( 'wcfmmp-stores', 'wcfm_sitemap_output' );
    }
}, 9 );

//Generate output for wcfmmp_sellers sitemap
function wcfm_sitemap_output() {
    global $WCFM, $wpseo_sitemaps;
    
    $timezone       = new WPSEO_Date_Helper();

    $seller_q = new WP_User_Query( array(
        'role'       => 'wcfm_vendor',
    ) );
    $sellers = $seller_q->get_results();
    ob_start();
    ?>
    <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <?php foreach ( $sellers as $seller ) {
            $products = $WCFM->wcfm_vendor_support->wcfm_get_products_by_vendor( $seller->ID, 'publish', array( 'posts_per_page' => 1 ) );
            if( !empty( $products ) ) {
                foreach( $products as $product ) {
                    $last_modified = $product->post_modified;
                    ?>
                    <url>
                        <loc><?php echo esc_url(wcfmmp_get_store_url( $seller->ID )) ?></loc>
                        <priority><?php echo esc_attr(apply_filters( 'wcfmmp_yoast_store_sitemap_priority', 0.8 ))  ?></priority>
                        <changefreq><?php echo esc_attr(apply_filters( 'wcfmmp_yoast_store_sitemap_changefreq', 'weekly' ))  ?></changefreq>
                        <lastmod><?php echo esc_attr($timezone->format( $last_modified )); ?></lastmod>
                    </url>
                    <?php break; 
                }
            } else {
                $userdata = get_userdata( $seller->ID );
                $last_modified = $userdata->user_registered;
                ?>
                    <url>
                        <loc><?php echo esc_url(wcfmmp_get_store_url( $seller->ID )) ?></loc>
                        <priority><?php echo esc_attr(apply_filters( 'wcfmmp_yoast_store_sitemap_priority', 0.8 ))  ?></priority>
                        <changefreq><?php echo esc_attr(apply_filters( 'wcfmmp_yoast_store_sitemap_changefreq', 'weekly' ))  ?></changefreq>
                        <lastmod><?php echo esc_attr($timezone->format( $last_modified )); ?></lastmod>
                    </url>
                <?php
            }
        }
        ?>
    </urlset>
    <?php
    $sitemap = ob_get_clean();
    $wpseo_sitemaps->set_sitemap( $sitemap );
}
m7mdharon

m7mdharon

Member