How to Automate the Insertion of Focus Keyword

#314558
  • Resolved Tomas Loucky
    Rank Math free

    Hello,

    I hope you’re having a good day! 🙂

    I’ve found the following Link about how to set up the automatic insertion of focus keywords. The reason for that is that I’m planning to use a lot of posts and it would be tedious to write it manually. I’d like to ask you what kind of keywords are generated, please. Does it pull the keywords randomly or does it analyze the post and select the right ones or something different? Just to know it is the right option to choose. Thank you 🙂

    Many thanks
    Tomas

Viewing 15 replies - 1 through 15 (of 26 total)
  • Anas
    Rank Math business

    Hello,

    Thank you for contacting Rank Math.

    The filter will add the Post Title, Post Tag, or Product Category as the focus keyword.

    Hope that helps.

    Don’t hesitate to get in touch if you have any other questions.

    Hello Anas,

    Yes, it does, then I will follow the tutorial. Thank you for your response and have a good day 🙂

    Best
    Tomas

    Just a quick question, please. Does it matter where do I insert the code? If it should be functions.php, styles.css or something else? I just wanted to be certain as the tutorial does not mention that. Thanks! 🙂

    Anas
    Rank Math business

    Hello,

    You should add the filter in the active theme’s functions.php file.

    Here is a guide you can follow:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/#functions-php-file

    As is with any code like this we recommend taking a full backup of your website and database before performing any action like this.

    I hope this helps. Looking forward to helping you.

    Hi Anas,

    Thank you for your response. I’ve done so but received the following message https://snipboard.io/E9v6zQ.jpg. Can you please advise me on what to do with that? Thank you very much.

    Best
    Tomas

    Hello,

    I have checked the screenshot and it seems you are trying to add the code twice.

    Please check 591 number line of functions.php file where you have already added the filter.

    Please refer to the screenshot:

    Make sure you have used the filter once in your active theme’s funtions.php file.

    Hope this helps. Let us know if you need any further assistance.

    Thank you.

    Hello Abdullah,

    Yes, I can confirm I’ve added code more than once. The reason is that I wanted to set the code for more custom post types. Can you please advise me on how to use the code for more than one post type? Or if it is even possible? Thank you very much.

    Best
    Tomas

    Hello,

    You have to modify the filter like this 'post_type' => 'yourposttype' and name the function differently and add it for your all post types separately.

    You can follow the following code and modify it as per your need.

    function update_focus_keywords() {
        $posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'yourposttype1' //replace post with the name of your post type
        ));
        foreach($posts as $p){
            update_post_meta($p->ID,'rank_math_focus_keyword1',strtolower(get_the_title($p->ID)));
        }
    }
    add_action( 'init', 'update_focus_keywords' );
    
    function update_focus_keywords2() {
        $posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'yourposttype2' //replace post with the name of your post type
        ));
        foreach($posts as $p){
            update_post_meta($p->ID,'rank_math_focus_keyword2',strtolower(get_the_title($p->ID)));
        }
    }
    add_action( 'init', 'update_focus_keywords1' );
    
    function update_focus_keywords3() {
        $posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'yourposttype3' //replace post with the name of your post type
        ));
        foreach($posts as $p){
            update_post_meta($p->ID,'rank_math_focus_keyword1',strtolower(get_the_title($p->ID)));
        }
    }
    add_action( 'init', 'update_focus_keywords3' );

    Hope this helps. Please do not hesitate to let us know if you need our assistance with anything else.

    Thank you.

    Hi Abdullah,

    Thank you for your advice.
    Can I please double-check with you that the following code is correct? I wanted to make a code for 5 custom post types. These post types are roles-find, roles-offer, locations, equipment and portfolio. I’m a bit unsure about the numbering of focus keywords within the code, such as what to change. Thank you very much.

    `

    function update_focus_keywords() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘roles-find’ //replace post with the name of your post type
    ));
    foreach($posts as $p){
    update_post_meta($p->ID,’rank_math_focus_keyword1′,strtolower(get_the_title($p->ID)));
    }
    }
    add_action( ‘init’, ‘update_focus_keywords’ );

    function update_focus_keywords2() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘roles-offer’ //replace post with the name of your post type
    ));
    foreach($posts as $p){
    update_post_meta($p->ID,’rank_math_focus_keyword2’,strtolower(get_the_title($p->ID)));
    }
    }
    add_action( ‘init’, ‘update_focus_keywords1’ );

    function update_focus_keywords3() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘locations’ //replace post with the name of your post type
    ));
    foreach($posts as $p){
    update_post_meta($p->ID,’rank_math_focus_keyword1′,strtolower(get_the_title($p->ID)));
    }
    }
    add_action( ‘init’, ‘update_focus_keywords3’ );

    function update_focus_keywords4() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘equipment’ //replace post with the name of your post type
    ));
    foreach($posts as $p){
    update_post_meta($p->ID,’rank_math_focus_keyword1′,strtolower(get_the_title($p->ID)));
    }
    }
    add_action( ‘init’, ‘update_focus_keywords5’ );

    function update_focus_keywords3() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘portfolio’ //replace post with the name of your post type
    ));
    foreach($posts as $p){
    update_post_meta($p->ID,’rank_math_focus_keyword1′,strtolower(get_the_title($p->ID)));
    }
    }
    add_action( ‘init’, ‘update_focus_keywords5’ );

    Hello,

    There were some numbering missmatch in your code. I have modified the code. You can use the following code in your active theme’s functions.php file:

    function update_focus_keywords() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘roles-find’ //replace post with the name of your post type
    ));
    foreach($posts as $p){
    update_post_meta($p->ID,’rank_math_focus_keyword1′,strtolower(get_the_title($p->ID)));
    }
    }
    add_action( ‘init’, ‘update_focus_keywords’ );
    
    function update_focus_keywords2() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘roles-offer’ //replace post with the name of your post type
    ));
    foreach($posts as $p){
    update_post_meta($p->ID,’rank_math_focus_keyword2’,strtolower(get_the_title($p->ID)));
    }
    }
    add_action( ‘init’, ‘update_focus_keywords2’ );
    
    function update_focus_keywords3() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘locations’ //replace post with the name of your post type
    ));
    foreach($posts as $p){
    update_post_meta($p->ID,’rank_math_focus_keyword3′,strtolower(get_the_title($p->ID)));
    }
    }
    add_action( ‘init’, ‘update_focus_keywords3’ );
    
    function update_focus_keywords4() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘equipment’ //replace post with the name of your post type
    ));
    foreach($posts as $p){
    update_post_meta($p->ID,’rank_math_focus_keyword4′,strtolower(get_the_title($p->ID)));
    }
    }
    add_action( ‘init’, ‘update_focus_keywords4’ );
    
    function update_focus_keywords5() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘portfolio’ //replace post with the name of your post type
    ));
    foreach($posts as $p){
    update_post_meta($p->ID,’rank_math_focus_keyword5′,strtolower(get_the_title($p->ID)));
    }
    }
    add_action( ‘init’, ‘update_focus_keywords5’ );

    Hope that helps. Let us know if you need any further assistance.

    Thank you.

    Hello Abdullah,

    That is perfect, thank you so much! Just a last quick question, I assume the keywords get pulled automatically after the creation of a new post. Is that correct? Thanks! 🙂

    Best
    Tomas

    Nigel
    Rank Math business

    Hello,

    The previous code you were given is meant for updating the focus keyword of old posts. It works for new posts too but is inefficient.

    The following code snippet works only when saving the current post:

    
    add_action( 'save_post', 'rm_set_save_post_title_focus_keyword',10,3 );
    
    function rm_set_save_post_title_focus_keyword ( $post_id, $post, $update ){
    	$accepted_post_types = ['roles-find', 'roles-offer', 'locations', 'equipment', 'portfolio'];
    	if (in_array( get_post_type( $post_id ), $accepted_post_types)){
    		update_post_meta($post_id,'rank_math_focus_keyword',strtolower(get_the_title($post_id)));
    	}
    }
    

    You can replace all the previous code you were given with the new snippet to update the focus keyword for only posts that you save/update.

    Hope that helps. Please let us know if you have questions.

    Hello Nigel,

    Thank you very much for your response. I can confirm that this code works better than the previous one. Could I please ask you an additional question? Is it please possible to pull the snippet from the specific meta field? Or does it get pulled from the excerpt? Also, Where are the keywords pulled from, please? It seems to me it pulls only the post title. Thank you 🙂

    Best
    Tomas

    Nigel
    Rank Math business

    Hello,

    The code I provided automatically pulls the focus keyword from the title. Depending on the specific metafield, it might be possible to to pull from there instead of the title. If you have a specific field you want to create the automatic keyword from, let us know and we’ll check whether it’s possible.

    If you have questions, ask away. We are here to help.

    Hello Nigel,

    This is great, thank you.
    I was just thinking that it could be beneficial to take advantage of all 5 keywords. So I was wondering whether it would be possible to add 4 more keywords to the title keyword. For example, if it could be set automatically to all the posts within the post type, meaning there would be the title keyword + 4 set keywords the same as the post type. I hope it makes sense. Thank you very much.

    Best
    Tomas

Viewing 15 replies - 1 through 15 (of 26 total)

The ticket ‘How to Automate the Insertion of Focus Keyword’ is closed to new replies.