-
Hi,
I have two questions on this article- https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/1. If there are multiple tags, are all tags to be inserted as focus keywords??
2. If I want to use a custom field value as a focus keyword, what will be the code??? (I have a user-submitted post website, so I wish to add a custom field for the focus key for front end use).
-
Hello,
Thank you for contacting Rank Math support today and bringing your concern to our attention.
1. Yes, if you notice in our filter code, there’s a piece of code that loops the entire tags which allows to insert all tags into your focus keyword field:
2. You can do something like this instead:
function update_focus_keywords() { $posts = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'post' )); foreach ($posts as $p) { global $post; $fromACF = get_field('my_field_name', $post->ID); /* Change my_field_name */ update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower($fromACF)); } } add_action('init', 'update_focus_keywords');
Just make sure that you changed the
my_field_name
to your desired custom field name.Let us know how that goes. Looking forward to helping you.
Hello, I would Like To Use Taxonomy Title as Focus Keyword in That Taxonomy Type?
Example: Let name my Taxonomy rankmath So How Can I use RankMath Taxonomy Title as my Focus Keyword?
I mean Instead of Post I want to deal with Taxonomy.. How Can I do that?
Thanks Hope to hear from you..
Hello @David,
Depending on the taxonomy you want to use, you can modify and use this filter:
https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/#automatically-use-the-tags-as-the-focus-keywordHope this helps.
Thank you.
I Have Modified it like below.. but it couldn’t work.. Did I do it Wrong.. my Taxonomy name is Episodes I want to use Taxonomy Titles as Focus Keyword
/** * Function to automatically update the focus keyword with the post title */ function update_focus_keywords() { $taxonomy = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'episodes' //replace post with the name of your post type )); foreach ($taxonomy as $p) { update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower(get_the_title($p->ID))); } } add_action('init', 'update_focus_keywords');
Hello @david-christian,
You are changing the post_type name into your taxonomy which will not work.
Please refer to the filter my colleague previously shared and change these line to:
... get_the_terms($p->ID, 'episodes') ...
Let us know if this helps you with your current issue.
Looking forward to helping you on this one.
Hi, Am not really good in PHP so it give me A little Headache
Here I try to Do it.. But it Fails WordPress Theme Editor it Tells Me it have Error so I can’t Edit..
Original Code :
/** * 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');
I try to Modify Them And Be This :
/** * 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' => 'series' //replace post with the name of your post type )); foreach ($posts as $p) { $keywords = []; if (get_the_terms($p->ID, 'episodes')) { foreach (get_the_terms($p->ID, 'episodes')) 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');
But WordPress It Give Me Error That my code is Wrong.. Can You Please Check Thanks.
These is how my URL for Editing Taxonomy ‘episodes’ look like may be it may help.
/wp-admin/edit-tags.php?taxonomy=episodes&post_type=series
Thanks, Hope to hear from you
In Modification
'post_type' => 'post'
I change to'post_type' => 'series'
And
if (get_the_tags($p->ID))
I change toif (get_the_terms($p->ID, 'episodes'))
Also
foreach ((get_the_tags($p->ID))
I change toforeach (get_the_terms($p->ID, 'episodes'))
Thank you.
URL for Episodes list on Backend it look like this
/wp-admin/edit-tags.php?taxonomy=episodes&post_type=series
URL For Editing Specific Episode it look like this
/wp-admin/term.php?taxonomy=episodes&tag_ID=14911&post_type=series&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Depisodes%26post_type%3Dseries
Hello,
You have an issue with your syntax that is causing a critical error. Please refer to this filter code instead:
function update_focus_keywords() { $posts = get_posts(array( 'posts_per_page' => 100, 'post_type' => 'series' //replace post with the name of your post type )); foreach ($posts as $p) { $keywords = []; if (get_the_terms($p->ID, 'episodes')) { foreach (get_the_terms($p->ID, 'episodes') 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');
Let us know how that goes. Looking forward to helping you on this one.
Hi,
Please look into my ticket #336618
It’s pending for a long duration!It Strange it ddn’t work for me.. @jeremy-4105
May I Know what things I can Ask My Theme Developers May be it may helps
Hello @David-Christian,
Please create a new ticket and explain the issue. The filter is customized for a CPT and will not work if you don’t have a CPT with the same name. Please change the
series
with the name of your CPT andepisodes
with the name of the taxonomy you are using.Looking forward to helping you.
Thanks.
Hello, I would like to automate the keywords for the taxonomies, that the name of the labels or categories be the KW.
For example, if a tag is called “big dogs” the keyword is big dogs, as simple as that.I tried with this code but I couldn’t, could you help me please.
function update_focus_keywords()
{
$posts = get_posts(array(
‘posts_per_page’ => 100,
‘post_type’ => ‘post_tag’ //replace post with the name of your post type
));
foreach ($posts as $p) {
$keywords = [];
if (get_the_terms($p->ID, ‘post_tag’)) {
foreach (get_the_terms($p->ID, ‘post_tag’) 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’);Hello @jjprime,
Your post_type is the same as your taxonomy which is ‘post_tag’. Can you please confirm? For us to further give advice, please let us if you are trying to implement the code to an actual taxonomy page? If so, please note that the code we provide only applies to post types.
Looking forward to helping you on this one.
I have this: /wp-admin/edit-tags.php?taxonomy=post_tag
/wp-admin/edit-tags.php?taxonomy=category
As I mentioned, I want the name of the taxonomies to be tags or categories to be keywords, can you help me with that code please, greetings.
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 ‘Automate the Insertion of Focus keyword’ is closed to new replies.