-
Hi,
My site wordlesstech.com has more than 20,000 posts.
I would like to insert tags as keywords for a part of the old posts.
For example for the year 2015, then 2016…
Is that possible?Thank you
Best regards
Agis F
-
Hello,
Thank you for contacting Rank Math support and we apologize for the inconvenience.
We understand that you want to simultaneously change the focus keyword for multiple posts. Yes, this is technically possible, but it requires some custom coding. We can help you with that, but we need to know your specific goal, i.e. posts from what year to what year do you want to change?
For example, here is a code snippet that can automatically update the focus keyword with the post tags for all your posts:
/** * Function to automatically update the focus keyword with the post tags */ function update_focus_keywords() { $posts = get_posts(array( 'posts_per_page' => 100, 'post_type' => 'post' //replace post with the name of your post type )); foreach ($posts as $p) { $keywords = []; if (get_the_tags($p->ID)) { foreach ((get_the_tags($p->ID)) as $tag) { $keywords[] = strtolower($tag->name); } update_post_meta($p->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords))); } } } add_action('init', 'update_focus_keywords');
You can copy and paste this code into your site using one of these methods: https://rankmath.com/kb/wordpress-hooks-actions-filters/
We hope this helps and makes things clearer. If you have any questions or issues, please let us know. We are always happy to help.
Thank you for choosing Rank Math!
Hi,
I would like to create the focus keywords for the posts of the year 2022.
Thank you for your support
Best
Agis F
Hello,
Thank you for getting back to us.
You can add a date query parameter to the get_posts function, like this:
/** * Function to automatically update the focus keyword of 2022 with the post tags */ function update_focus_keywords() { $posts = get_posts(array( 'posts_per_page' => 100, 'post_type' => 'post', //replace post with the name of your post type 'date_query' => array( //add this parameter to filter posts by date array( 'year' => 2022, //only get posts from the year 2022 ), ), )); foreach ($posts as $p) { $keywords = []; if (get_the_tags($p->ID)) { foreach ((get_the_tags($p->ID)) as $tag) { $keywords[] = strtolower($tag->name); } update_post_meta($p->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords))); } } } add_action('init', 'update_focus_keywords');
This should update the focus keyword with the post tags only for posts created in the year 2022. Please try adding this code to your site and let us know if it fulfilled your requirement.
If you have any questions or issues, please let us know.
Thank you for choosing Rank Math!
Hi,
I will get back to you in a couple of days.
Thank you
Best
Agis F
Hello,
Sure! Let us know how it goes.
Meanwhile, please don’t hesitate to let us know if you have any other questions.
Looking forward to helping you.
Thank you.
Hello,
Please keep us updated on your progress with the provided solutions. We eagerly await your response.
To ensure you have ample time and support, we’ll keep this support ticket open until your issue is fully resolved.
Please don’t hesitate to get in touch if you have any other questions.
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 ‘Auto creating Keywords’ is closed to new replies.