1. Home
  2. Knowledge Base
  3. SEO Suite
  4. How to Automate the Insertion of Focus Keyword

How to Automate the Insertion of Focus Keyword

In this knowledgebase article, we’ll show you how you can automate the insertion of focus keywords for your content.

Automatically Use the Post Title as the Focus Keyword

You can use this code in WordPress > Appearance > Theme Editor > functions.php to automatically use the post title as the focus keyword:

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' );

Automatically Use the Product Category as the Focus Keyword

This code can be used in WordPress > Appearance > Theme Editor > functions.php to use the product category as focus keyword in WooCommerce Products:

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){
		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' );

 

Search engines prefer that you add your focused keywords in your title, description, and throughout your content. However, there might be situations where your focus keyword might not be saved. Rank Math makes it very easy for you to set a focus keyword and optimize your content for it. If you have any other questions about focus keywords and how they work in Rank Math, don’t hesitate to reach out to our support team.

Still not using Rank Math?

Setup takes less than 5 minutes including the import from your old SEO Plugin!

Know more about the PRO Version

Was this article helpful?

Still need help?

?

Submit Your Question

Please give us the details, our support team will get back to you.

Open Ticket

Related Articles