Help in Focus Keyword Automation

#453783
  • Resolved Delonifera TV
    Rank Math free

    I went through the blog posts on how to automate focus keywords, i tried to add the below snippet to my functions.php

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

    it did not work and crashing my blog. So i tried to add it through creating rank-math.php file in my themes folder but its still crashing my blog.

    I have over 50k posts with no focus keywords and i will love to automate this.

    Thanks

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

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

    To address it, you need to set an offset so you can run the code at a given limit to conserve hardware resources since you have 50k posts which is a lot of posts to process.

    Please refer to the conversation here for further guidance: https://support.rankmath.com/ticket/can-i-set-bulk-keywords/?view=all

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

    Thanks so much for your quick response. Setting offset keeps crashing the blog so i later use the below code

    function update_focus_keywords() {
    $posts = get_posts(array(
    ‘posts_per_page’ => 500,
    ‘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’ );

    500 posts per page, but i don’t understand how to loop this. like to keep adding keywords based on title without my consent….. I input the code inside my function.php putting it i rank-math.php is returning error.

    Also, how can i check or see how many posts have focus keywords and how many doesn’t so that i can watch the progress

    thanks.

    Thanks i think its working now, i set the post per page to 1000 but i’m not including the offset since its throwing out error when i do so. I will like to leave the code and will this keep working and automate the focus keywords? We run a blog with minimum 500 posts per day from different authors all around the world, we will want to leave this and automate the focus keyword.

    I hope this will keep working without my consent?

    If not, please let me know what to add to make it work automate.

    Thanks for your support.

    Hello,

    Automation-wise, yes, this will automate the first 1000 pages that are currently shown on the page list in the admin dashboard so most likely, the new posts will be automated.

    Please note that this code will only be triggered when the page list page loads so if you wish to include the older posts, you need to adjust the limit and offset.

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

    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 4 replies - 1 through 4 (of 4 total)

The ticket ‘Help in Focus Keyword Automation’ is closed to new replies.