How to auto add focus keywords of all posts for only First 2 words.

#570198
  • Resolved Anonymous
    Rank Math free

    Hello dear i need a script or settings in which it can auto add focus keywords of first 2 words of the title for example:

    Title= “Jennifer white is here in london meet her…”

    So i want the focus keyword= “Jennifer white”, not the full title.

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

    Thank you for contacting us and bringing your concern to our attention.

    You can try to add the following filter on your website to add only first two keywords of your title as your focus keyword:

    /**
     * Function to automatically update the focus keyword with the first two words of 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) {
            $title = strtolower(get_the_title($p->ID));
            $words = explode(' ', $title);
            $focus_keyword = $words[0];
            if (count($words) > 1) {
                $focus_keyword .= ' ' . $words[1];
            }
            update_post_meta($p->ID, 'rank_math_focus_keyword', $focus_keyword);
        }
    }
    add_action('init', 'update_focus_keywords');
    

    Here’s how you can add filter/hook to your WordPress site: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Anonymous
    Rank Math free

    Can you do this for me? I will provide the admin access?

    Hi there,

    Thank you for reaching out.

    We might not be able to apply a custom code to your site on your behalf, but if you grant us access to your site with FTP, then we might be able to do a local test on your site and provide you with detailed steps to take to do this yourself.

    To provide us access to your website please follow these steps:

    We appreciate your cooperation and patience. Thank you for choosing Rank Math.

    Anonymous
    Rank Math free

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Hello,

    We have added the filter code using the credentials provided with the help of “Code Snippets” plugin successfully.

    We hope that helps, and please don’t hesitate to get in touch if you have any other questions.

    Thank you.

    Anonymous
    Rank Math free

    Thank you so much dear

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)

The ticket ‘How to auto add focus keywords of all posts for only First 2 words.’ is closed to new replies.