Focus keyword for product category

#775568
  • Resolved Anonymous
    Rank Math free

    Hi,

    I have a small query on this I am using below code to automatically update focus keywords for products
    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’ );

    But to automatically update focus keyword for product category can i know what extra line of code i have to add

    Thanks
    Pavan

Viewing 1 replies (of 1 total)
  • Hello,

    Thank you for reaching out with your query. To automatically update the focus keyword for product categories, you can use the following code snippet:

    /**
     * Function to automatically update the product focus keyword with the product category, if no focus keyword is set
     */
    function update_product_focus_keywords()
    {
        $products = get_posts(array(
            'posts_per_page' => 100,
            'post_type' => 'product' //replace post with the name of your post type
        ));
        foreach ($products as $p) {
            $keywords = [];
            if (get_the_terms($p->ID, 'product_cat')) {
                foreach (get_the_terms($p->ID, 'product_cat') as $term) {
                    $keywords[] = strtolower($term->name);
                }
                if (!get_post_meta($p->ID, 'rank_math_focus_keyword', true)) {
                    update_post_meta($p->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords)));
                }
            }
        }
    }
    add_action('init', 'update_product_focus_keywords');

    This code will automatically update the focus keyword for product categories if no focus keyword is set. Feel free to add this code snippet to your functions.php file or rank-math.php file as needed.

    If you have any more questions or need further assistance, please let us know.

    Thank you!

    Sources:
    How to Automate the Insertion of Focus Keyword » Rank Math
    How to Automate the Insertion of Focus Keyword » Rank Math
    How to Automate the Insertion of Focus Keyword » Rank Math
    How to Automate the Insertion of Focus Keyword » Rank Math
    How to Automate the Insertion of Focus Keyword » Rank Math

    This is an automated reply from Rank Math Support Bot. If you need further assistance, please reply to this topic and a human support agent will reply to you shortly.

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

The ticket ‘Focus keyword for product category’ is closed to new replies.