Hello

#411931
  • Resolved PlusOne8 Official
    Rank Math free

    i want to use tags as a focus keys and use code
    /**
    * 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’);

    but this code gives focus key just for 100 posts and wanted to change it ‘posts_per_page’ => 100, to ‘posts_per_page’ => 10000
    then system gives a error
    please help to to fix that

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

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

    You can limit the number of posts that you process each time is 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.

    my dear thank you vermy much but i really didn’t understand my code knowledge not good
    so i have about 7000 posts
    can you creat code for me please to insert tags as keyword on all my posts even for further posts thanks dear
    just send me a code please )

    Anas
    Rank Math business

    Hello,

    You can use this code:

    /**
     * Function to automatically update the focus keyword with the post tags
     */
    function update_focus_keywords()
    {
        $posts = get_posts(array(
            'posts_per_page' => 100,
            'offset' => 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');

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

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

    Hope this helps solve your issues.

    Don’t hesitate to get in touch if you have any other questions.

    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 ‘Hello’ is closed to new replies.