Hello,
Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for the delay and for any inconvenience this issue may have caused you.
You can try this optimized code for your ad_listing post type:
function update_focus_keywords() {
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'ad_listing' //replace post with the name of your post type
));
foreach($posts as $p){
if(get_the_tags($p->ID)){
foreach((get_the_tags($p->ID)) as $tag) {
$keywords[] = strtolower($tag->name);
}
update_post_meta($p->ID,'rank_math_focus_keyword',implode(', ', array_unique($keywords)));
}
}
}
add_action( 'init', 'update_focus_keywords' );
And this filter should run in your theme’s functions.php file.
I hope that helps. Thank you, and looking forward to your update.
Hello Reinelle,
Thank you for kind. I have two different post type (ad_listing and post ). I need to edit the command to run both(post, ad_listing) at the same time.
Thank you again.
Hello,
You can add multiple post type in your function by adding this part array( 'post', 'ad_listing' ).
Please replace your code with the code below.
function update_focus_keywords() {
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => array( 'post', 'ad_listing' ) //replace post with the name of your post type
));
foreach($posts as $p){
if(get_the_tags($p->ID)){
foreach((get_the_tags($p->ID)) as $tag) {
$keywords[] = strtolower($tag->name);
}
update_post_meta($p->ID,'rank_math_focus_keyword',implode(', ', array_unique($keywords)));
}
}
}
add_action( 'init', 'update_focus_keywords' );
Please let us know if that helps.
Thank you.
Hello,
We are super happy that this issue is already resolved. If you have any other questions in the future, know that we are here to help you.
If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.
If you have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
Thank you.