Custom Code Snippet

#396658
  • Resolved John Doey
    Rank Math free

    Hi, please can you provide me the code to automatically convert Woocommerce products tags to focus keywords. The only close one available on the support page was post tags, and product categories

Viewing 8 replies - 1 through 8 (of 8 total)
  • Anas
    Rank Math business

    Hello,

    Thank you for contacting Rank Math.

    You can use this code to add the Product tag as a focus keyword for your products:

    function update_product_focus_keywords()
    {
        $products = get_posts(array(
            'posts_per_page' => 100,
            'post_type' => 'product' //replace post with the name of your post type
        ));
        foreach ($products as $p) {
            $keywords = [];
            if (get_the_terms($p->ID, 'product_tag')) {
                foreach (get_the_terms($p->ID, 'product_tag') as $term) {
                    $keywords[] = strtolower($term->name);
                }
                if (!get_post_meta($p->ID, 'rank_math_focus_keyword', true)) {
                    update_post_meta($p->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords)));
                }
            }
        }
    }
    add_action('init', 'update_product_focus_keywords');

    We recommend taking a complete backup of your website before adding this code.

    Hope that helps.

    Thank you.

    Hi. Thanks for getting back to me. I tried the code, but it didn’t work

    Hello,

    We might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress login in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Hi. Thanks for reaching out. The code works after I cleared my cache

    Hello. Thanks for the snippet, but I’d wanted it to update existing focus keyword. What this code does is it adds to the ones that are empty. I want it to replace all.

    Thank you!

    Hello,

    We removed the condition that checks for the existence of keywords and it should now update all the entries.

    Here’s the final code:

    
    function update_product_focus_keywords()
    {
        $products = get_posts(array(
            'posts_per_page' => 100,
            'post_type' => 'product' //replace post with the name of your post type
        ));
        foreach ($products as $p) {
            $keywords = [];
            if (get_the_terms($p->ID, 'product_tag')) {
                foreach (get_the_terms($p->ID, 'product_tag') as $term) {
                    $keywords[] = strtolower($term->name);
                }
                update_post_meta($p->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords)));
            }
        }
    }
    add_action('init', 'update_product_focus_keywords');
    

    Hope this helps solve your issues.

    Don’t hesitate to get in touch if you have any other questions.

    Thank you so much. It worked beautifully!

    Hello,

    Glad that helped.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help.

    Thank you.

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

The ticket ‘Custom Code Snippet’ is closed to new replies.