-
I created rank-math.php and put in wp-contenttheme folder to automate focus keyword for post from tags
following https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/Now I want to do the same for Products.
how to do that?
please help
Thanks
-
Hello,
Thank you for contacting Rank Math and bringing your concern to our attention.
The main differences are
'post_type' => 'product'
and replacing theget_the_terms
function with theget_the_tags
function. Please try this modified filter:/** * Function to automatically update the product focus keyword with the product category */ function update_product_focus_keywords() { $products = get_posts(array( 'posts_per_page' => 100, 'post_type' => 'product' //replace post with the name of your post type )); foreach ($products as $p) { $keywords = []; if (get_the_tags($p->ID, 'product_tag')) { foreach(get_the_tags($p->ID, 'product_tag') as $term) { $keywords[] = strtolower($term->name); } update_post_meta($p->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords))); } } } add_action('init', 'update_product_focus_keywords');
Let us know how this goes.
Thank you.
It did not work.. Below is my code
<?php
/**
* Function to automatically update the focus keyword with the post tags, if no focus keyword is set
*/
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);
}
if (!get_post_meta($p->ID, ‘rank_math_focus_keyword’, true)) {
update_post_meta($p->ID, ‘rank_math_focus_keyword’, implode(“, “, array_unique($keywords)));
}
}
}
}
add_action(‘init’, ‘update_focus_keywords’);/**
* Function to automatically update the product focus keyword with the product category
*/
function update_product_focus_keywords()
{
$products = get_posts(array(
‘posts_per_page’ => 100,
‘post_type’ => ‘product’ //replace post with the name of your post type
));
foreach ($products as $p) {
$keywords = [];
if (get_the_tags($p->ID, ‘product_tag’)) {
foreach(get_the_tags($p->ID, ‘product_tag’) as $term) {
$keywords[] = strtolower($term->name);
}
update_post_meta($p->ID, ‘rank_math_focus_keyword’, implode(“, “, array_unique($keywords)));
}
}
}
add_action(‘init’, ‘update_product_focus_keywords’);?>
Hello,
I noticed a mistake in the second (WooCommerce product) code snippet.
/** * Function to automatically update the product focus keyword with the product category */ function update_product_focus_keywords() { $products = get_posts(array( ‘posts_per_page’ => 100, ‘post_type’ => ‘product’ //replace post with the name of your post type )); foreach ($products as $p) { $keywords = []; if (get_the_terms($p->ID, ‘product_tag’)) { foreach(get_the_terms($p->ID, ‘product_tag’) as $term) { $keywords[] = strtolower($term->name); } update_post_meta($p->ID, ‘rank_math_focus_keyword’, implode(“, “, array_unique($keywords))); } } } add_action(‘init’, ‘update_product_focus_keywords’);
The snippet above should work. The main differerence is the ‘get_the_tags’ function is replaced with the ‘get_the_terms’ function.
Hope that helps. If you have questions, do not hesitate to ask.
No.. it did not work either.
Hello,
We might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.
Please do take a complete backup of your website before sharing the information with us.
It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:
https://wordpress.org/plugins/temporary-login-without-password/
You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):
https://wordpress.org/plugins/wp-security-audit-log/
We really look forward to helping you.
Hello,
I have updated the sensitive data as requested. Can you please check further?
Thank you.
Added the WP login and FTP. Please check
Hello,
I wasn’t able to locate the rank-math.php file on your site. However, I have added the code given below to your theme’s functions.php file and the tags are being added as the focus keyword for your products:
/** * Function to automatically update the product focus keyword with the product category */ function update_product_focus_keywords() { $products = get_posts(array( 'posts_per_page' => 100, 'post_type' => 'product' //replace post with the name of your post type )); foreach ($products as $p) { $keywords = []; if (get_the_terms($p->ID, 'product_tag')) { foreach(get_the_terms($p->ID, 'product_tag') as $term) { $keywords[] = strtolower($term->name); } update_post_meta($p->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords))); } } } add_action('init', 'update_product_focus_keywords');
Please check and let us know if you need any other assistance.
As I mentioned at start of post I have put the rank-math.php in folder put in wp-content \ theme folder .
Is this not the place to put the file ??
And when you put the code in functions.php… will this survive the theme upgrades?
Thanks
RanjeetAnd you are right the rankmath.php file is not there now !
And I had my theme updated today.. so it means it was removed by the update.
Assuming that even the functions.php file will be overwritten in next update.
How to overcome that?
Thanks
Hello,
The rank-math.php file should be added inside your active theme folder. In your case, that would be
wp-content/themes/rehub-theme
.The rank-math.php file will not be overwritten by theme updates. If you decide to add a snippet to your functions.php make sure it’s a child theme and that the child theme is active.
I was unable to connect FTP as the details were incomplete. Please include the ftp server address and port if you want us to add the snippet for you.
Hope that helps. Please let us know if you have questions.
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 ‘product focus keyword’ is closed to new replies.