Exclude keyword in function “auto add focus keyword”

#487552
  • Resolved IMK
    Rank Math free

    Hello,

    I am currently using the following function to automatically populate the focus Keyword field.

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

    However, if I edit any focus kewyrod manually, I cannot save the change.

    I turn to the excellent developers at Rank Math….

    Is there a way that if the focus keyword field is filled in that the function does not continue to automatically put the title of the post or page?

    Regards

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

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

    This will prevent overwriting the keywords you have manually filled into your posts.

    Hope that helps.

    Thank you.

    IMK
    Rank Math free

    Googd moorning Reinelle,

    Thank you for your prompt response. The code is working correctly.

    Very kindly 🙂

    Hello,

    Glad that helped.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help.

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Exclude keyword in function “auto add focus keyword”’ is closed to new replies.