Getting Error if I am using the the below script

#559555
  • Resolved Anonymous
    Rank Math free

    Automatically Use the Product Category as the Focus Keyword
    This code can be used in functions.php or rank-math.php in WordPress Dashboard > Appearance > Theme File Editor (for a Classic Theme) or WordPress Dashboard > Tools > Theme File Editor (for a Block Theme) to use the product category as focus keyword in WooCommerce Products:

    /**
    * Function to automatically update the product focus keyword with the product category, if no focus keyword is set
    */
    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_cat’)) {
    foreach (get_the_terms($p->ID, ‘product_cat’) 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’);

Viewing 3 replies - 1 through 3 (of 3 total)
  • Anonymous
    Rank Math free
    Anonymous
    Rank Math free
    Nigel
    Rank Math business

    Hello,

    We’re sorry for any inconvenience caused and appreciate your understanding and patience.

    Assuming you pasted the code in rank-math.php, you may need to add https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Please get in touch if you need further assistance.

    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 ‘Getting Error if I am using the the below script’ is closed to new replies.