Unable to change focus keywords after adding automatic focus keywords code

#224818
  • Resolved Sachin Tiwari
    Rank Math pro

    Hello there I have added the below code as provided in your documentation , but after adding that I’m not able to change the focus keywords:

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

    I want to change focus keywords of some of the posts as per my requirements.
    Please let me know if its possible.

    Regards

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

    Hello,

    As the focus keywords are added for all the posts, you can now remove the code.

    Once you remove the code, you will be able to edit the focus keyword on individual posts.

    If you keep the code in your functions.php file, you will not be able to edit the focus keyword.

    I hope this helps. Looking forward to helping you.

    Hello Anas,

    Thanks for your reply, but this isn’t the solution.

    Cause If I add the code again then the focused keywords which I have entered gets removed and gets replaced by post title.

    Is it possible to do it with some funtions to add focused keyword only if its not there already.

    Regards

    Anas
    Rank Math business

    Hello,

    Please replace the previous code with the below one and see if this gives you the desired result.

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

    I hope this helps. Looking forward to helping you.

    Worked like a charm!!

    Thank you very much for your support.

    Regards

    Anas
    Rank Math business

    Hello,

    We are super happy that your issue is resolved. 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.

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

The ticket ‘Unable to change focus keywords after adding automatic focus keywords code’ is closed to new replies.