-
HI, I tried automate insertion of focus keyword
but not working.
1. focus keyword is set with titles but not SEO updated.
2. it errors because of “rank-math.php file” so I deleted.
-
Hello,
I apologize for the issue you are facing with automating the insertion of focus keywords. Thank you for raising this concern.
It seems like there might be a couple of issues at play here:
- If the focus keyword is set with titles but not updating the SEO, it could be due to caching or other settings in your Rank Math configuration. Make sure to clear any cache and check your settings to ensure everything is configured correctly.
- Regarding the error with the
rank-math.php
file, if you deleted it, that could cause further issues. It’s important to have that file in place for the code snippets to work properly.
If you would like to automate the insertion of focus keywords again, you can refer to the following code snippets:
To set the title of your post as the focus keyword:
/** * 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' => 'post' //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');
To set focus keywords only for posts that don’t have a focus keyword set:
/** * 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){ $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' );
Please ensure that you add this code back into the
rank-math.php
file correctly. If you encounter any further issues or errors, please reply to this ticket with your site URL so we can assist you better. A human agent from our support team will take over and help resolve your issue.Thank you for your patience!
Sources:
How to Automate the Insertion of Focus Keyword » Rank Mathwhen I surge Cache, this errors occured.
“/** * 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’ => ‘post’ //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 restored from my backup.
then tried from the beginning.1. not Set focus keyword.
2. site crushed like image.siteground optimizer also error,
Rank math SEO recalculator stuck. not working
https://imgur.com/a/ps5d8iZfocus keyword set : done
but SEO Score : N/A
https://imgur.com/a/9UIRz3wRecalculation SEO Score : Somethings went Wrong Message
https://imgur.com/a/SExiOhAHello,
It happens since this line in the filter reads/processes all your posts, which can slow down your site or even generate errors if you have many posts and pages on your site:
'posts_per_page' => -1
If this is affecting your site due to PHP memory or resource limitations, you can limit the number of posts that you process each time by setting a limit on the posts like so:
'offset' => 100
You can continue to increase the offset by 100 until it runs through all of the posts.
Each time you run the function, you can add 100 to the offset value like,
1st time : 'offset' => 0, 2nd time : 'offset' => 100, 3rd time : 'offset' => 200,
And so on…
Also, an ongoing issue with our Recalculate Score feature has already been reported to our development team, and we are working on a fix.
We will notify everyone via the usual communication channels once this is fixed and added to our update.
We really appreciate your kind patience with this matter.
Looking forward to helping you.
Hi, Thank you for your explanation.
I’m wordpress beginner.
where do I put these? rank-math.php?
then where?‘posts_per_page’ => -1
‘offset’ => 100
1st time : ‘offset’ => 0,
2nd time : ‘offset’ => 100,
3rd time : ‘offset’ => 200,Hello,
The following filter which you can enter in the rank-math.php file or follow any of these methods in this guide: https://rankmath.com/kb/wordpress-hooks-actions-filters/
/** * 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' => 100, 'offset' => 0, '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' );
Simply replace the line
'offset' => 0,
with'offset' => 100,
after running it the first time and subsequently replace with'offset' => 200,
and then'offset' => 300,
and so forth until the focus keyword is added to all posts.Please note the starting offset should be 0, and the code will add the focus keyword for the first 100 posts. Then you will increase the offset to 100, and the code will add the keyword to 100 more posts. So you will have to keep increasing the offset by 100 until it has added the code to all the posts. Once all the posts have the keyword, you can remove the code.
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Hi,
I’ve put this code to rank-math.php with ‘offset’=>0
but even if I purge cache, Flush SEO analyzer data, Remove Rank math Transients it’s still same
https://imgur.com/a/9stpqjhIf offset is 0, it says that 100 seo Score will be updated, but even if I check the very beginning and the end of the post, it is still N/A.
How do I run it?
Hello,
You should edit some of your posts to check the keywords if they are already showing in the editor.
Regarding the Score Recalculate issue, this was already fixed in the latest Beta release, and will be pushed to the next update soon.
If you would like to update it now, you can follow the steps here:
https://rankmath.com/kb/version-control/#beta-updatesLooking forward to helping you.
beta version resolved it
thanksHello,
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.
The ticket ‘HI, I tried automate insertion of focus keyword’ is closed to new replies.