-
Hello,
My site has more than 50K products, so I needed a solution to generate Rank Math Focus Keyword Automatically and found your code on your website.
https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/
I have a code snippets plugin installed, so I applied one of your codes using the Code Snippets.
/**
* Function to automatically update the focus keyword with the post title
*/
function update_focus_keywords()
{
$posts = get_posts(array(
‘posts_per_page’ => -1,
‘post_type’ => ‘product’ //replace post with the name of your post type
));
foreach ($posts as $p) {
update_post_meta($p->ID, ‘rank_math_focus_keyword’, strtolower(get_the_title($p->ID)));
}
}
add_action(‘init’, ‘update_focus_keywords’);I only changed ‘post’ -> ‘product’ because I wanted to use the product title as a keyword and all the product has the post_type of ‘product’.
Somehow the code broke my website, and I lost access to the wp-admin.
So I needed to log in with the code snippet ‘safe’ mode to remove the code.The code seemed to work when I applied it to my testing website, which has much fewer products, but it broke the live site.
Would you help me if there are some errors with this code?
Could using ‘product’ instead of ‘post’ cause the problem?
I’m not a PHP expert, but I can read some code, so I thought it made sense if I put ‘product’ instead of ‘post’.Would you let me know the right code in case I want to put the product title as a keyword?
I’ll really appreciate all your support if you can suggest me a solution.Thank you!
The ticket ‘Problem Generating Focus Keyword Automatically’ is closed to new replies.