Hello,
Thank you for your query and we are so sorry about the trouble this must have caused.
The only possible way of adding additional URLs to the sitemap is by using the following filter: https://rankmath.com/kb/filters-hooks-api-developer/#add-extra-urls-in-sitemap
To achieve this dynamically you’ll need to create a loop that iterates over the products and finds all the variations to add inside the sitemap.
This would require some extensive custom coding to achieve and as such is outside the scope of our support.
Don’t hesitate to get in touch if you have any other questions.
Hi Miguel,
thanks for your reply.
Basicly there are just three attributes that are used for product variations to create them:
attribute_pa_groesse
attribute_pa_farbe-schonbezug
attribute_pa_waermeklasse
WP Rocket is able to store cache as soon as product variation got’s first visit.
Once I clean the cache to refresh the page, cache is gone for product variation and is taking the longer initial loading time which I want to avoid.
In order to get this done I found this code, to include product variations into RankMath Sitemap:
/**
* Funktion zum Hinzufügen von benutzerdefinierten Feldern zu RankMath-Sitemaps für Produkte
*/
function custom_add_product_fields_to_sitemap() {
// Überprüfen, ob RankMath installiert und aktiviert ist
if (defined('RANK_MATH_VERSION')) {
// Produkt-Query
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
$products = new WP_Query($args);
// Überprüfen, ob Produkte gefunden wurden
if ($products->have_posts()) {
while ($products->have_posts()) {
$products->the_post();
// Produkt-Attribute abrufen
$groesse = get_post_meta(get_the_ID(), 'attribute_pa_groesse', true);
$farbe_schonbezug = get_post_meta(get_the_ID(), 'attribute_pa_farbe-schonbezug', true);
$waermeklasse = get_post_meta(get_the_ID(), 'attribute_pa_waermeklasse', true);
// Produkt-URL erstellen
$product_url = get_permalink();
// RankMath-Funktion zum Hinzufügen von Daten zur Sitemap aufrufen
RankMath\Sitemap\Generators\Product::add(
$product_url,
array(
'custom_field_groesse' => $groesse,
'custom_field_farbe_schonbezug' => $farbe_schonbezug,
'custom_field_waermeklasse' => $waermeklasse,
)
);
}
// Stelle sicher, dass der Original-WordPress-Post zurückgesetzt wird
wp_reset_postdata();
}
}
}
// Hook, um die Funktion aufzurufen
add_action('init', 'custom_add_product_fields_to_sitemap')
But Iam not sure If this right?
Kind regards,
Daniel
Hello,
We are not certain if that code will work as you are not using the filter code that we recently shared.
Can you please try it out on your website? If you don’t want to try it on a live site, you can do so on a staging site.
Please note that this type of customization falls outside the scope of our support so we can’t assist you beyond giving the placeholder filter code.
With that said, if the code doesn’t work, you may try adding the loop inside this filter/action code:
add_action( 'rank_math/sitemap/product_content', function() {
...
});
Don’t hesitate to get in touch with us if you have any other questions.
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.