Hello,
Thank you for contacting Rank Math.
You can use the code for the topics and replies, you only need to set the post_type to topic and reply in the code.
/**
* 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' => 'reply' // 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' );
Here is how to add a filter to your site: https://rankmath.com/kb/wordpress-hooks-actions-filters/
Hope this helps.
Don’t hesitate to get in touch if you have any other questions.
Hi
Can I use both TOPIC and REPLY at the same time in the same piece of code ?
Hello,
Yes, you can use both post types.
You can add multiple post types like this:
'post_type' => ['reply','topic']
Hope that helps.
Thank you.
Hi.
One more question if I may, can the above code be added to either my themes Functions.php or Rank-math.php and can that be left to run in the background or does it have to be removed after the Focus keywords have been added ?
Thank you
Hello,
If you want to keep adding the focus keyword automatically to the new posts, you can keep the filter active on your site. If not, you can remove it once the keywords are added to the existing posts. However, it is recommended to remove the filter once it has done its job and added the keywords to the existing posts.
Hope that helps. Let us know if you need any other assistance.
Hi
Understood and as always, thank you for your guidance
Hello,
We are super happy that we could address your concern. 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 do 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.