Issue with Automating Focus Keywords

#196654
  • Resolved Ade A
    Rank Math pro

    Dear RankMath Support Team,

    I am trying to implement the guide shared at https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/

    My intend was to auto-insert tags as focus keywords for those posts without any focus keywords, however, I noticed the following issues:

    1. The script took the tags in the latest post and inserted them into existing posts that already have focus keywords. This implies that all posts so far updated have the same focus keywords against the initial intention. Please refer to the image here: https://ibb.co/LZvw81L

    2. When I set the “‘posts_per_page’ => -1,” the script did not work and broke my site until I removed the codes. When I changed to “‘posts_per_page’ => 100,” the script worked but resulted to the error above.

    Please guide me aright.

    Thank you.

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

    Thank you for contacting the support, and sorry for any inconvenience that might have been caused due to that.

    1. Do you mind sharing your code here so we can take a closer look at your goal of automating focus keywords?

    2. Setting the posts_per_page to -1 will loop the posts you have on your website. I am assuming you have a lot of posts to process and setting this value to -1 will surely use so much hardware resulting in memory or CPU exhaustion.

    We are looking forward to helping you.

    Thanks for your prompt response.

    I used the following:

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

    Hello,

    Thank you for keeping in touch with us.

    You need to re-initialize or empty the $keyword variable every time you loop your posts to avoid distributing all keywords to all the posts.

    Please try this code instead:

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

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    I will check the new code.

    Few questions please.

    1. What does “posts_per_page” do? If I put 100 for instance, does that mean only 100 posts will be updated?

    2. I have close to 100k posts. What is the best way to update them using the codes?

    3. After all posts have been appended focus keywords, do I need to remove the codes?

    4. What should I do if after an attempt, all the posts are not updated?

    Thanks

    Hello,

    1. Basically, yes. With this configuration, the code should retrieve all 100 posts and you can loop to it to distribute the keywords. You can learn more about it here: https://developer.wordpress.org/reference/functions/get_posts/

    2. Since you are dealing with these massive numbers, you may need to modify your code to make it more “CPU-friendly”. You can set an offset every time you run the code until you added the KWs to all the posts. This might still require hard work but this is one way to do it. You may refer to to this forum: https://wordpress.stackexchange.com/questions/13368/get-posts-from-post-xoffset-x-to-end

    3. If you are done adding all the Keywords, it is safe to remove the code. The keywords will not be deleted after doing so.

    4. The code you are trying to use should add the keywords from the tags, please ensure your posts have tags on them and it should be updated. You may also do a hard refresh on the browser and see if the KWs are added.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Thanks for your response. I will check the resources recommended and revert for further help if required.

    Hello,

    Thanks for getting back.

    We will keep this ticket open until you confirm that your issue is resolved. Let us know if you need any further assistance with this.

    Looking forward to hearing from you.

    Hello,

    Is it possible to exclude a case where focus keywords already exist while running the code?

    Thanks.

    Hi,

    I have been able to update all the keywords to a great extent.

    I wanted to ask can the score be updated on the overall post page. The SEO Scores are showing N/A, however, when I attempted to edit the post, I could see the real score showing up.

    Thank you for your help!

    Hello,

    Thank you for keeping in touch with us.

    It is normal to see “N/A” in the post/page list. The scoring script only runs when you open a post. Please open a post, make changes, see the correct score, save the page. The score will then propagate to the Post List view as well.

    Note that this will not affect your actual SEO settings on your front page.

    Unfortunately, there is no way to automate this. We would not suggest automating that anyway.

    Some of the tests only run when you load up a post. The score gets saved when you hit the update button. But, don’t just hit the update button without changing any content on your website. If you do this, Google will think you are trying to manipulate the date Google sometimes shows beside your search result (https://i.rankmath.com/BiZDrV)

    So, we recommend that you start with your most important posts, open then, make changes as per recommendations made by Google and then hit the update button.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Hi Jeremy,

    Thank you for reply and explanations.

    I asked a question before the last one. This is recalled below:

    Is it possible to exclude a case where focus keywords already exist while running the code?

    Appreciate your team effort.

    Cheers

    Hello,

    Please try to use this filter instead and see if this allows you to exclude posts that has already Keywords in them:

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

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    I will try it. Thank you.

    Anas
    Rank Math business

    Hello,

    Let us know how it goes.

    Meanwhile, if you have any other questions, please feel free to ask.

    We are here to help you.

    Thank you for your help. I think this specific issue is resolved.

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

The ticket ‘Issue with Automating Focus Keywords’ is closed to new replies.