Autofocus key word

#772131
  • Resolved Anonymous
    Rank Math free

    Hi
    I have added the following code to the Rankmath.php file
    /**
    * Function to automatically update the focus keyword with the post title, if no focus keyword is set
    */
    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){
    // 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’ );

    But focus keyword was not being added

    Your kind assistance in this matter is highly appreciated

    Thanks
    Pavan

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

    I’m sorry to hear that you are facing issues with adding the focus keyword even after adding the code to the Rankmath.php file.

    Based on the information provided, it seems like you might be encountering an issue related to adding the focus keyword. To troubleshoot this matter effectively, I recommend checking if you have set a specific page as your homepage under WordPress Dashboard > Settings > Readings > Your homepage displays. If you have not set a specific page as your homepage, you will not be able to add focus keywords to it using Rank Math.

    If you have already set a specific page as your homepage and are still facing issues, I suggest reaching out to the Rank Math support team for further assistance. You can contact them through their support page: Rank Math Support.

    Thank you for bringing this concern to our attention. We appreciate your patience and understanding as we work to resolve this issue for you.

    If you have any more questions or need further assistance, please feel free to let us know. We’re here to help!

    Sources:
    Analyzing Your Site’s SEO with Rank Math’s SEO Analysis Tool
    Score 100/100 With Rank Math Post Tests » Rank Math
    Rank Math’s Content Analysis Tests For Archive Pages » Rank Math
    Bulk Editing Options in the Rank Math SEO Plugin

    Hello,

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

    The filter code you’ve shared is correct. However, can you please confirm if you have a large number of posts on your website? If so, you can try to use the following filter on your website and see if that works for you:

    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){
            // 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' );
    

    Please make sure to increase the offset value by 100 each time you run the code.

    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

    Hi

    I am trying to set this for products

    I have around 1400 products

    https://crystalnails.ie

    I am using code snippet plugin as well Can i create a new snippet with above code or should i only add this to the rannkmath php file

    Your kind assistance in this matter is highly appreciated

    Thanks
    pavan

    Hello,

    For products, please make sure to change the post type as below:

    'post_type'		=> 'product' 
    

    However, if you have the Code Snippet plugin plugin on your website, you can add the filter there instead of the rank-math.php file.

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

    Thank you.

    Anonymous
    Rank Math free

    HI,
    Thanks for your reply
    I am unable to attach a screenshot here

    but following is the error code that i got while trying to add a new snippet
    Please let me know what mistake i am doing

    Snippet automatically deactivated due to an error on line 2:
    Cannot redeclare function update_focus_keywords.

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

    Thanks
    Pavan

    Hello,

    Thank you for your patience.

    You can upload screenshots using this tool and add the link here.

    That error occurs when you attempt to define a function with a name that has already been declared earlier in the code.

    Please ensure that you’re using the code once only as the function name should be unique.

    If you’re still unable to apply the filter, then we can do it for you by logging in to your site.

    To do that, please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.

    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Anonymous
    Rank Math free

    Hello,

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

    Thank you.

    Hello,

    It looks like you were trying to apply 2 filter codes at a time on your website. However, we deactivated the previous one and applied the modified filter on your website and now the focus keyword has been updated for all of your products.

    Hope that helps and please don’t hesitate to let us know if you have any other questions.
     
    Thank you.

    Anonymous
    Rank Math free

    Hello,

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

    Thank you.

    Anonymous
    Rank Math free

    HI,

    Thanks for the update

    Much appreciate your help

    I have changed this on the main site and working correctly now

    Thanks
    Pavan

    Hello,

    We are glad to hear that this issue has been resolved. Thank you for letting us know. This ticket will be closed now, but you can always open a new one if you have any other questions or concerns. We are here to help you with anything related to Rank Math.

    We appreciate your patience and cooperation throughout this process.

    Thank you for choosing Rank Math.

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

The ticket ‘Autofocus key word’ is closed to new replies.