Hello,
We apologize for the issue you are facing with the code for automating the insertion of focus keywords. To assist you better, could you please provide more details about what specifically isn’t working?
In the meantime, here are some common troubleshooting steps:
- Ensure that you have added the code snippet to the correct file, which is typically
rank-math.php
in your theme’s directory.
- Check if there are any syntax errors in the code that might prevent it from executing properly.
- Make sure that your posts or products actually meet the conditions set in the code (e.g., having no existing focus keyword).
If you need further assistance, please reply to this ticket, and a human agent from the Rank Math Support team will take over and help you resolve the issue. Thank you for raising this concern!
This is an automated reply from Rank Math Support Bot. Your ticket has been assigned to a human agent and we will get back to you shortly.
https://i.imgur.com/tJVcmCJ.png
I am trying to use the following code to automatically update the “Focus Keyword” in Rank Math SEO for my WordPress posts by using the post title as the keyword. However, despite adding the code to the rank-math.php file, the keywords are not being updated for any posts. I have also tried debugging, but I could not identify the issue.
Here is the code I am using:
/**
* Function to automatically update the focus keyword with the post title, if no focus keyword is set
*/
function update_focus_keywords() {
$posts = get_posts(array(
‘posts_per_page’ => -1,
‘post_type’ => ‘post’ // Replace post with the name of your post type
));
foreach ($posts as $p) {
// Checks if Rank Math keyword already exists and only updates if it doesn’t have it
$rank_math_keyword = get_post_meta( $p->ID, ‘rank_math_focus_keyword’, true );
if ( ! $rank_math_keyword ) {
update_post_meta( $p->ID, ‘rank_math_focus_keyword’, strtolower( get_the_title( $p->ID ) ) );
}
}
}
add_action( ‘init’, ‘update_focus_keywords’ );
Hello,
Thank you for contacting us. We apologize for any inconvenience this issue may have caused.
It appears you have not added the opening tag for the PHP snippet.
Please add the opening PHP tag to the first line of the file: ;lt&?php
![https://imgur.com/BBqjhj1](https://imgur.com/BBqjhj1.png)
Let us know how it goes. Looking forward to helping you.
Thank you.