Error auto add focus keyword in post type

#476926
  • Resolved IMK
    Rank Math free

    Hello,

    I am trying to add automatically the “focus keyword” as meta name keyword.

    I am using the snippet of the following url:

    https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/

    I have added the snippet for the “posts” and it works correctly.

    However after creating and adding a new snippet for the “pages” the snippet does not work and gives me an error.

    I am sharing both codes:

    Focus Kws Posts – WORK

    /**
    * Function to automatically update the focus keyword with 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) {
    update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower(get_the_title($p->ID)));
    }
    }
    add_action('init', 'update_focus_keywords');

    ————————————

    Focus Kws Pages – NOT WORK

    /**
    * Function to automatically update the focus keyword with the post title
    */
    function update_focus_keywords()
    {
    $pages = get_posts(array(
    'posts_per_page' => -1,
    'post_type' => 'page' //replace post with the name of your post type
    ));
    foreach ($pages as $p) {
    update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower(get_the_title($p->ID)));
    }
    }
    add_action('init', 'update_focus_keywords');

    Please I would appreciate your help.

    Thanks

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.

    I’ve tested the filter you have shared for automating keywords in pages, but it seems to be working fine.

    Could you please share the error message you have upon running the filter?

    Looking forward to helping you.

    Thank you.

    IMK
    Rank Math free

    Hello Reinelle,

    Thank you for your prompt reply.

    I think I have found the solution.

    Because I added two snippets with the same function name: function update_focus_keywords(), apparently it was giving error.

    By changing the name of the function, it worked.

    Thanks for your help

    Hello,

    Thank you for the update.

    We are glad that you have already managed to resolve this issue.

    If you have any other concerns, please don’t hesitate to contact us anytime to assist you further.

    Looking forward to helping you.

    Thank 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 ‘Error auto add focus keyword in post type’ is closed to new replies.