I want to automate the additional keywords too?

#632103
  • Resolved Anonymous
    Rank Math free

    I have seen the code on in the tutorials to automatically at the post title as the keyword, but I was wondering if it’s possible to add additional ones too?

    Basically I want it to add the post title and then again with additional words as keywords/phrases

    This is what I tried but to no avail –

    /**
    * Function to automatically update the focus keyword and additional keywords for death-notice post type
    */
    function update_death_notice_keywords()
    {
    $death_notices = get_posts(array(
    ‘posts_per_page’ => -1, // Get all death-notice posts
    ‘post_type’ => ‘death-notice’
    ));

    foreach ($death_notices as $notice) {
    $keywords = [];

    // Get the title (name of the deceased)
    $deceased_name = strtolower($notice->post_title);
    $keywords[] = $deceased_name; // Add the deceased name as the focus keyword

    // Add additional keywords
    $additional_keywords = array(
    “{$deceased_name} death notice”,
    “{$deceased_name} funeral”,
    “{$deceased_name} funeral details”,
    “{$deceased_name} death funeral”
    );

    $keywords = array_merge($keywords, $additional_keywords);

    // Update the focus keyword and additional keywords using Rank Math functions
    update_post_meta($notice->ID, ‘rank_math_focus_keyword’, $deceased_name);
    update_post_meta($notice->ID, ‘rank_math_related_keywords’, implode(“, “, array_unique($keywords)));
    }
    }

    // Hook the function to run during WordPress initialization
    add_action(‘init’, ‘update_death_notice_keywords’);

    Is it possible?

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

    Thank you for contacting Rank Math support, and sorry for any inconvenience that might have been caused due to that.

    Instead of using rank_math_related_keywords, you can instead stick with rank_math_focus_keyword as your target when adding multiple keywords.

    Since the main keyword ($deceased_name) is already added in the $keywords[] array, you can simply follow the below line to update the meta:

    
    update_post_meta($notice->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords)));

    Hope that helps and please do not hesitate to let us know if you need my assistance with anything else.

    Anonymous
    Rank Math free

    Hi Jeremy,

    Thank you for the swift reply.

    I’ll be honest though, you’ve kind of lost me as I’m just sure exactly how to adjust the code based on what you said? Would you be able to add the line in where it should be please?

    Really appreciate your help,
    Joe

    Hello,

    Please try this updated filter on your site:

    /**
    * Function to automatically update the focus keyword and additional keywords for death-notice post type
    */
    function update_death_notice_keywords()
    {
    $death_notices = get_posts(array(
    'posts_per_page' => -1, // Get all death-notice posts
    'post_type' => 'death-notice'
    ));
    
    foreach ($death_notices as $notice) {
    $keywords = [];
    
    // Get the title (name of the deceased)
    $deceased_name = strtolower($notice->post_title);
    $keywords[] = $deceased_name; // Add the deceased name as the focus keyword
    
    // Add additional keywords
    $additional_keywords = array(
    "{$deceased_name} death notice",
    "{$deceased_name} funeral",
    "{$deceased_name} funeral details",
    "{$deceased_name} death funeral"
    );
    
    $keywords = array_merge($keywords, $additional_keywords);
    
    // Update the focus keyword and additional keywords using Rank Math functions
    update_post_meta($notice->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords)));
    }
    }
    
    // Hook the function to run during WordPress initialization
    add_action('init', 'update_death_notice_keywords');

    Let us know how this goes.

    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 ‘I want to automate the additional keywords too?’ is closed to new replies.