Problem Generating Focus Keyword Automatically

#521015
  • Resolved AVLgear
    Rank Math free

    Hello,

    My site has more than 50K products, so I needed a solution to generate Rank Math Focus Keyword Automatically and found your code on your website.

    https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/

    I have a code snippets plugin installed, so I applied one of your codes using the Code Snippets.

    /**
    * 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’ => ‘product’ //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 only changed ‘post’ -> ‘product’ because I wanted to use the product title as a keyword and all the product has the post_type of ‘product’.

    Somehow the code broke my website, and I lost access to the wp-admin.
    So I needed to log in with the code snippet ‘safe’ mode to remove the code.

    The code seemed to work when I applied it to my testing website, which has much fewer products, but it broke the live site.

    Would you help me if there are some errors with this code?
    Could using ‘product’ instead of ‘post’ cause the problem?
    I’m not a PHP expert, but I can read some code, so I thought it made sense if I put ‘product’ instead of ‘post’.

    Would you let me know the right code in case I want to put the product title as a keyword?
    I’ll really appreciate all your support if you can suggest me a solution.

    Thank you!

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

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

    I’ve used the following code on my localhost and it is working perfectly without any issue:

    function update_focus_keywords()
    {
        $posts = get_posts(array(
            'posts_per_page'    => -1,
            'post_type'        => 'product' //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');
    

    Can you please share the website error log with us?

    Having the error log file will give a clear idea of why this might be happening.

    You can follow this guide to enable debugging in WordPress
    https://wordpress.org/support/article/debugging-in-wordpress/ or you can get in touch with your web host, they will help you locate the log file.

    Looking forward to helping you.

    Thank you.

    ​​​​​​​

    Thank you for your response.

    So, if I want to see the error log generated, shouldn’t I re-produce the same error to get the log?
    I almost broke my website activating the code and it’s a live site getting orders everyday.
    Do I understand right? so I need to re-produce the error which can cause the access denial to the wp-admin.
    I’m afraid that I can’t recover it this time.

    So, only changing ‘post’ to ‘product’ from the code should work. right?

    The code line below is used in the code for ‘Automatically Use the Tags as the Focus Keyword’. I found some differences, and the following line is one of them.

    ‘posts_per_page’ => 100,

    vs

    ‘posts_per_page’ => -1 —> This line is used for our case.

    Couldn’t this cause the error? I just wonder.

    Would you help me with the way that doesn’t need to re-produce the access denied error, if it’s possible?

    Thank you!

    Hello,

    Yes, changing the post type to “product” should work.

    In the filter, this line reads/process all your posts, which can slow down your site and throw the error:
    'posts_per_page' => -1

    However, you can limit the number of posts that you process each time by setting a limit on the posts like so:

    'posts_per_page' => 100
    

    You can then add an offset to the function as well to continue adding the focus keywords to newer posts. So, after changing the first 100 posts you add an offset of 100 to the function like so:

    'offset' => 100
    

    You can continue to increase the offset by 100 until it runs through all of the posts.

    Each time you run the function, you can add 100 to the offset value like,

    1st time : 'offset' => 0, 
    2nd time : 'offset' => 100,
    3rd time : 'offset' => 200,

    And so on…

    I hope that helps.

    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 ‘Problem Generating Focus Keyword Automatically’ is closed to new replies.