Include Query Product-Pages into Sitemap for WP-Rocket Preloading

#653046
  • Resolved Daniel
    Rank Math free

    Hello!

    I have a simple question which I hope, you can help me out:
    Is it possible to create or to add to the already existing product sitemap Query Pages?

    The reason why Iam asking is because Product Variations are not preloaded by WP Rocket and they say that there is no way to make it. So my approach is to achieve that by using RankMath Sitemap since it’s the base for caching.

    What I exactly mean:
    Parent Product URL: https://store.com/product/parent-product/ is in the sitemap.
    Child Product URL: https://store.com/product/parent-product/?attribute_pa_size=200cm is not included in the sitemap.

    Can you provide a snippet where it is possible to declare which attributes are added to products sitemap.

    I’ve already read you article (https://rankmath.com/kb/configure-sitemaps/) but could not find any solution.

    Thank you and kind regards,
    Daniel

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

    Daniel
    Rank Math free

    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.

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

The ticket ‘Include Query Product-Pages into Sitemap for WP-Rocket Preloading’ is closed to new replies.