Hello,
Thank you for contacting Rank Math and bringing your concern to our attention.
You can use the following filter on your site to automatically use your post categories as the focus keyword:
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){
if(get_the_category($p->ID)){
foreach((get_the_category($p->ID)) as $category) {
$keywords[] = strtolower($category->name);
}
update_post_meta($p->ID,'rank_math_focus_keyword',implode(", ", array_unique($keywords)));
}
}
}
add_action( 'init', 'update_focus_keywords' );
Here is a guide you can follow to add this filter:
https://rankmath.com/kb/wordpress-hooks-actions-filters/
Let us know how it goes.
Thank you.
Hi Rakibuzzaman,
thank you, that worked. But it overloaded my Server for an extensive period of time, resulting in massive outages for hours. I’ve since there deactivated this snippet. Is there maybe a chance to only use it in the feature upon save_post?
Hello,
In the filter, this line reads/process all your posts, which can slow down your site and throw the error:
'posts_per_page' => -1
However, you can limit the number of posts that you process each time by setting a limit on the posts like so:
'posts_per_page' => 100
You can then add an offset to the function as well to continue adding the focus keywords to newer posts. So, after changing the first 100 posts you add an offset of 100 to the function 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…
I hope that helps.
Thank you.
As i mentioned before, all Keywords for all 3.500+ Posts have been already set. Its just for future Posts so is there an alternative that applies those Filters to save_post or when it is published? It is not necessary to apply this filter to old/already published Posts again.
Hello,
We trigger this with the init
action but you can try to run the function to update the focus keywords during the save_post
method and only use it for the current post being saved.
This sort of high-level modifications are outside the scope of our support so you would need to hire a developer to change the filter if you don’t feel familiar with PHP coding.
Don’t hesitate to get in touch if you have any other questions.
Okay, i’ve now used the first filter and replaced init with save_post. Thank you.
Hello,
Glad that everything has been sorted out.
Please feel free to reach out to us again in case you need any other assistance.
We are here to help.
Thank you.