-
hi, currently i am using rank math for SEO. However, before, I had many products that did not have the focus keyword. I want to add batch automation. I tried inserting the code into the function, but it didn’t work. Please help me. Thank you.
-
Hello,
Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.
Can you please confirm if you were trying to use any filter from the URL below?
https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/If so, then please share the filter with us so we can check the code and advise if any changes are needed for that to work.
Looking forward to helping you.
Thank you.
Hello,
Thank you for contacting the support and sorry for any inconvenience that might have been caused due to that.
You can add this code in theme’s function.php
/** * 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');
You can read more about it here: https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Thank you
Hi, I have applied the code to my website. However it only works with new posts, and my old posts and products are not updated.
Hello,
The code snippet we provided in the previous reply is designed to work on all existing posts existing posts but may time out if you have hundreds/thousands of posts on your website. If it seems like the code snippet is only working on newer posts, it’s because the code is timing out. To avoid time outs, you will have to run the code in batches. To do that, you will need to change the line
'posts_per_page' => -1,
in the snippet to'posts_per_page' => 100,
this will make the code snippet run for only the first 100 posts in your database.To update the focus keywords of the next 100 posts, you will need to add the line
'offset' => 100,
below the line'posts_per_page' => 100,
to set the focus keywords for the second batch of 100 posts. After the second batch you will need to increment the offset by batches of 100 to get the rest of your posts. below is the full code snippet for the third batch:/** * Function to automatically update the focus keyword with the post title */ function update_focus_keywords() { $posts = get_posts(array( 'posts_per_page' => 100, 'offset' => 200, //3rd batch has offset 200, 4th batch has offset 300 etc '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');
You can simply refresh any page on your website to run the code each time. When you have gone through all the posts, you may delete the snippet.
Hope that helps. Please let us know if you have questions.
ok i did and it worked. thank you very much
Hello,
We are glad it worked for you.
If you have any other concerns, please don’t hesitate to contact us anytime to assist you further.
Looking forward to helping you.
Thank you.
Hi, I have one more question. If I want to automatically update both products and posts, how should I enter the code?
Hello,
Please try to replace the filter code with the following one and see if that works for both products and posts:
function update_focus_keywords() { $post_types = array('product', 'post'); $posts = get_posts(array( 'posts_per_page' => 100, 'offset' => 200, 'post_type' => $post_types, )); 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');
Let us know how it goes. Looking forward to helping you.
Thank you.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.
The ticket ‘focus keyword’ is closed to new replies.