Advertisement
wclovers

Yoast Sitemap - Index all Stores even without products

Jun 16th, 2023
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. add_action( 'init', function() {
  2.     global $WCFMmp, $wpseo_sitemaps;
  3.     remove_action( 'init', [ $WCFMmp->wcfmmp_store_seo, 'register_sitemap' ] );
  4.  
  5.     if ( is_a( $wpseo_sitemaps, 'WPSEO_Sitemaps' ) ) {
  6.         $wpseo_sitemaps->register_sitemap( 'wcfmmp-stores', 'wcfm_sitemap_output' );
  7.     }
  8. }, 9 );
  9.  
  10. //Generate output for wcfmmp_sellers sitemap
  11. function wcfm_sitemap_output() {
  12.     global $WCFM, $wpseo_sitemaps;
  13.    
  14.     $timezone       = new WPSEO_Date_Helper();
  15.  
  16.     $seller_q = new WP_User_Query( array(
  17.         'role'       => 'wcfm_vendor',
  18.     ) );
  19.     $sellers = $seller_q->get_results();
  20.     ob_start();
  21.     ?>
  22.     <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">
  23.         <?php foreach ( $sellers as $seller ) {
  24.             $products = $WCFM->wcfm_vendor_support->wcfm_get_products_by_vendor( $seller->ID, 'publish', array( 'posts_per_page' => 1 ) );
  25.             if( !empty( $products ) ) {
  26.                 foreach( $products as $product ) {
  27.                     $last_modified = $product->post_modified;
  28.                     ?>
  29.                     <url>
  30.                         <loc><?php echo esc_url(wcfmmp_get_store_url( $seller->ID )) ?></loc>
  31.                         <priority><?php echo esc_attr(apply_filters( 'wcfmmp_yoast_store_sitemap_priority', 0.8 ))  ?></priority>
  32.                         <changefreq><?php echo esc_attr(apply_filters( 'wcfmmp_yoast_store_sitemap_changefreq', 'weekly' ))  ?></changefreq>
  33.                         <lastmod><?php echo esc_attr($timezone->format( $last_modified )); ?></lastmod>
  34.                     </url>
  35.                     <?php break;
  36.                 }
  37.             } else {
  38.                 $userdata = get_userdata( $seller->ID );
  39.                 $last_modified = $userdata->user_registered;
  40.                 ?>
  41.                     <url>
  42.                         <loc><?php echo esc_url(wcfmmp_get_store_url( $seller->ID )) ?></loc>
  43.                         <priority><?php echo esc_attr(apply_filters( 'wcfmmp_yoast_store_sitemap_priority', 0.8 ))  ?></priority>
  44.                         <changefreq><?php echo esc_attr(apply_filters( 'wcfmmp_yoast_store_sitemap_changefreq', 'weekly' ))  ?></changefreq>
  45.                         <lastmod><?php echo esc_attr($timezone->format( $last_modified )); ?></lastmod>
  46.                     </url>
  47.                 <?php
  48.             }
  49.         }
  50.         ?>
  51.     </urlset>
  52.     <?php
  53.     $sitemap = ob_get_clean();
  54.     $wpseo_sitemaps->set_sitemap( $sitemap );
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement