Change category to titke product in focus keyword

#539881
  • Resolved Soloparamiweb
    Rank Math free

    Hello, I add this code in rank-math.php and is allright
    ————————————————————–
    <?php
    /**
    * Function to automatically update the product focus keyword with the product category
    */
    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);
    }
    update_post_meta($p->ID, ‘rank_math_focus_keyword’, implode(“, “, array_unique($keywords)));
    }
    }
    }
    add_action(‘init’, ‘update_product_focus_keywords’);
    ——————————————————————-

    My problem: I need to add NAME of Product, NO category product in focus keyword.

    Thank yoy very much

Viewing 1 replies (of 1 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.

    The filter you have applied indeed adds the product category instead of the product name.

    In this case, please use this filter instead:

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

    Hope that helps.

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

The ticket ‘Change category to titke product in focus keyword’ is closed to new replies.