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