-
Hello,
I am trying to add automatically the “focus keyword” as meta name keyword.
I am using the snippet of the following url:
https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/
I have added the snippet for the “posts” and it works correctly.
However after creating and adding a new snippet for the “pages” the snippet does not work and gives me an error.
I am sharing both codes:
Focus Kws Posts – WORK
/**
* Function to automatically update the focus keyword with the post title
*/
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) {
update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower(get_the_title($p->ID)));
}
}
add_action('init', 'update_focus_keywords');————————————
Focus Kws Pages – NOT WORK
/**
* Function to automatically update the focus keyword with the post title
*/
function update_focus_keywords()
{
$pages = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'page' //replace post with the name of your post type
));
foreach ($pages as $p) {
update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower(get_the_title($p->ID)));
}
}
add_action('init', 'update_focus_keywords');Please I would appreciate your help.
Thanks
The ticket ‘Error auto add focus keyword in post type’ is closed to new replies.