-
Good day. I have inserted your suggested snippet to automatically add focus keyword based on the title of a custom post (animals). This works fine. However, I would also like to do the same for another CPT (plants).
What I tried was inserting a second snippet for the plants with new function name.
Animals snippet:
/**
* Function to automatically update the focus keyword with the post title, if no focus keyword is set
*/
function update_animal_focus_keywords() {
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'animals' // 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_animal_focus_keywords' );Plants snippet:
/**
* Function to automatically update the focus keyword with the post title, if no focus keyword is set
*/
function update_plant_focus_keywords() {
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'plants' // 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_plant_focus_keywords' );This seems to work, but if both snippets are enabled, it messes up the display of my featured image in the side panel. It just doesn’t display. As soon as I turn off either of the 2 snippets, all is well again.
Perhaps I need to use one snippet that references both post types?
The ticket ‘Automating Focus Keyword snippet’ is closed to new replies.