-
Hello,
I don’t know what’s wrong.
But neither of these codes is working properly.
https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/Thank you.
-
Hello,
I’m sorry to hear that you’re experiencing issues with the code provided in the article. It seems that you’re having trouble with the code snippets mentioned in the article. To better assist you, could you please provide more details about the specific issues you’re encountering with the code? This will help us understand the problem and provide you with the most accurate solution.
Thank you for bringing this to our attention.
Sources:
How to Automate the Insertion of Focus Keyword » Rank Math
Score 100/100 With Rank Math Post Tests » Rank Math
How Does Search Engine Rank Tracking Work? » Rank MathHello,
you have 6 codes in the article.
Only the one that manually adds the keyword to the rank marh after publishing works.
This is the 2nd code in the order.Well I would like to autoamtize this work and add them autoamtically.
That doesn’t work anymore.My *biggest* problem is that I have a recipe website. And I have a dedicated section where I add recipes.
So I have a different “post type”.
I use the EasyMeals theme.Is it possible to change this so that it works for my recipes?
Thank you.Hello,
Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.
Could you please confirm which filter you were trying to use on your website so that we can check the issue further for you?
However, if you want to use the filter for multiple post types then you can modify the post_type like the following one:
$post_types = array('post', 'product'); // Add your post types here $posts = get_posts(array( 'posts_per_page' => -1, 'post_type' => $post_types, ));
Let us know how it goes. Looking forward to helping you.
Thank you.
Good day,
I tried to add this code.
I edited the post type but unsuccessfully.“Please note the above code will replace all your existing focus keywords too. In case, if you wish to set the post title as a focus keyword only for the posts that don’t have a focus keyword set, then use the following code instead .”
/**
* Function to automatically update the focus keyword with the post title, if no focus keyword is set
*/
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){
// Checks if Rank Math keyword already exists and only updates if it doesn’t have it
$rank_math_keyword = get_post_meta( $p->ID, ‘rank_math_focus_keyword’, true );
if ( ! $rank_math_keyword ){
update_post_meta($p->ID,’rank_math_focus_keyword’,strtolower(get_the_title($p->ID)));
}
}
}
add_action( ‘init’, ‘update_focus_keywords’ );Well thank you.
Hello,
Thank you for your patience.
We’ve checked your site, but couldn’t find the code you have inserted as your theme file editor seems disabled.
However, from the post type in your site, please try this updated filter:
/** * Function to automatically update the focus keyword with the post title, if no focus keyword is set */ function update_focus_keywords() { $posts = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'EasyMeals Recipes' // Replace post with the name of your post type )); foreach($posts as $p){ // Checks if Rank Math keyword already exists and only updates if it doesn't have it $rank_math_keyword = get_post_meta( $p->ID, 'rank_math_focus_keyword', true ); if ( ! $rank_math_keyword ){ 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 this goes.
Problem fix.
Fixx,, again..
Hello,
Glad that helped.
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.
The ticket ‘How to Automate the Insertion of Focus Keyword’ is closed to new replies.