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

If your website consists of numerous posts without focus keywords, then adding them one by one could be tedious. So in this knowledgebase article, we’ll show you how you can automate the insertion of focus keywords into your content.

Automatically Use the Post Title as the Focus Keyword

To set the title of your post as the focus keyword, you can add the following code in your theme’s functions.php or rank-math.php by navigating to WordPress Dashboard > Appearance > Theme Editor.

/**
 * Function to automatically update the focus keyword with the post title
 */
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');

Please note the above code will replace all your existing focus keywords too. In case, if you wish to set the post title as a focus keyword only for the posts that don’t have a focus keyword set, then use the following code instead.

/**
 * 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'		=> 'post' // 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' );

Automatically Use the Tags as the Focus Keyword

To set all your posts with the post tags as focus keywords, you can add the following code in functions.php or rank-math.php under WordPress Dashboard > Appearance > Theme Editor.

/**
 * Function to automatically update the focus keyword with the post tags
 */
function update_focus_keywords()
{
    $posts = get_posts(array(
        'posts_per_page' => 100,
        'post_type' => 'post' //replace post with the name of your post type
    ));
    foreach ($posts as $p) {
        $keywords = [];
        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');

The above code will replace any existing focus keyword as well. In case, if you wish to set focus keywords only for the posts that don’t have any focus keyword set, then use the following code instead.

/**
 * Function to automatically update the focus keyword with the post tags, if no focus keyword is set
 */
function update_focus_keywords()
{
    $posts = get_posts(array(
        'posts_per_page' => 100,
        'post_type' => 'post' //replace post with the name of your post type
    ));
    foreach ($posts as $p) {
        $keywords = [];
        if (get_the_tags($p->ID)) {
            foreach ((get_the_tags($p->ID)) as $tag) {
                $keywords[] = strtolower($tag->name);
            }
            if (!get_post_meta($p->ID, 'rank_math_focus_keyword', true)) {
                update_post_meta($p->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords)));
            }
        }
    }
}
add_action('init', 'update_focus_keywords');

Automatically Use the Product Category as the Focus Keyword

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

/**
 * Function to automatically update the product focus keyword with the product category
 */
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');

 

The above code will replace any existing focus keyword with the product category. If you wish to set the focus keyword only for the WooCommerce products that don’t have any focus keyword set, then you may use the following code.

/**
 * Function to automatically update the product focus keyword with the product category, if no focus keyword is set
 */
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);
            }
            if (!get_post_meta($p->ID, 'rank_math_focus_keyword', true)) {
                update_post_meta($p->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords)));
            }
        }
    }
}
add_action('init', 'update_product_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.

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