我发现了以下使用类别作为焦点关键字的代码片段,但我不知道如何将其更改为在焦点关键字中包含标题。
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_cat’)) {
foreach(get_the_terms($p->ID, ‘product_cat’) 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’);
如果没有设置焦点关键字,则使用帖子标题自动更新焦点关键字的功能请试试这个。
function update_focus_keywords() {
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'product' // 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' );