i added the script to make the automation for the focus keyword

#514629
  • Resolved Cristian prodan
    Rank Math free

    I have a problem with the automation for the focus keyword, i used this script to add the tags as focus keyword:

    /**
    * Function to automatically update the focus keyword with the post tags
    */
    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’);

Viewing 3 replies - 1 through 3 (of 3 total)
  • Nigel
    Rank Math business

    Hello,

    Thank you for contacting Rank Math for help with automating the insertion of focus keywords.

    I checked the code snippet you shared, and it looks correct. It will set tags as the focus keywords for the first 100 posts in your database. Please may you specify what challenge you are facing with focus keywords.

    Looking forward to helping you.

    is not working this is the problem). i need to set for all my products (3400)

    Hello,

    In this case, please set an offset since you are only calling 100 products or you can set the posts_per_page to -1, however, this might consume all your memory resulting in a downtime of your website.

    If you wish to proceed with using offset, please refer to this thread here: https://support.rankmath.com/ticket/cant-upload-code-to-change-all-keywords-to-the-same-as-title/?view=all#post-493348

    Let us know how that goes. Looking forward to helping 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 ‘i added the script to make the automation for the focus keyword’ is closed to new replies.