custom Field for Focus Keywords

#962848
  • Resolved Naveen Parmuwal
    Rank Math free

    I have created custom field named ”focus_keywords” (screenshot-https://snipboard.io/3uIxqT.jpg and https://snipboard.io/mScQ8d.jpg) with the help of Advanced Custom Fields Plugin to add multiple keywords in rank math focus keywords from custom field text box, for new draft and publish articles.

    I Got this below code from Rank math AI chat box and I added in rank-math.php

    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 ) {
    // Replace ‘focus_keywords’ with your ACF field name.
    if ( ! get_post_meta( $p->ID, ‘rank_math_focus_keyword’, true ) && function_exists( ‘get_field’ ) && ( null !== get_field( ‘focus_keywords’, $p->ID ) ) ) {
    update_post_meta( $p->ID, ‘rank_math_focus_keyword’, strtolower( get_field( ‘focus_keywords’, $p->ID ) ) );
    }
    }
    }
    add_action( ‘init’, ‘update_focus_keywords’ );

    But it’s not working.

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

    We apologize for the issue you are facing with the custom field for focus keywords. Thank you for providing the code and details about your setup.

    The code snippet you provided seems to be structured correctly for updating the Rank Math focus keyword from your ACF field named focus_keywords. However, there are a few things you might want to check:

    1. Ensure ACF is Active: Make sure that the Advanced Custom Fields plugin is active and that the custom field focus_keywords is correctly set up for the post type you are targeting.

    2. Check Post Status: The code will only run on published posts. If you are trying to update drafts, you may need to adjust the query to include drafts as well. You can modify the get_posts function to include drafts by adding 'post_status' => 'any':

      $posts = get_posts(
         array(
             'posts_per_page' => -1,
             'post_type'      => 'post', // replace post with the name of your post type.
             'post_status'    => 'any',
         )
      );
    3. Debugging: You can add some debugging statements to check if the function is being called and if it retrieves the correct values from your custom field. For example, you can log the values or use error_log() to see if the function is executing as expected.

    4. Hook Priority: Sometimes, changing the priority of the add_action can help. You can try using a higher priority like this:

      add_action( 'init', 'update_focus_keywords', 10 );
    5. Check for Conflicts: Ensure that there are no other plugins or themes that might be conflicting with this functionality.

    If after checking these points it still does not work, please reply to this ticket, and a human agent from the Rank Math Support team will take over and assist you further.

    Thank you for your patience!

    Sources:
    Is There a Way to Use ACF for Focus Keywords? » Rank Math
    How to Automate the Insertion of Focus Keyword » Rank Math

    Hello,

    Thank you for your query and we are so sorry about the trouble this must have caused.

    That code seems to be working fine from our end. See this video screencast: https://i.rankmath.com/v/urFpSV

    Can you please make sure the filter code is correctly inserted to your website using any of this method: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    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 2 replies - 1 through 2 (of 2 total)

The ticket ‘custom Field for Focus Keywords’ is closed to new replies.