Hello,
Thank you for contacting Rank Math.
You need to set the post_type to product in the code to add the focus keyword to your products.
'post_type' => 'product'
Hope this helps.
We look forward to helping you.
Thank you for your replay
I modified the post_type to product, the code of “automatically Use the Post Title and the Product Category as the Focus Keyword” worked, but the tags code still does not work for me
Hello,
Since you’re working on products, you may need to update the get_the_tags and tags as well.
Please use this filter instead:
/**
* Function to automatically update the product focus keyword with the product category
*/
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 that helps.
Thank you.
Thank u very much, it worked perfectly for me
Hello,
We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.
If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.
If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
Thank you.