How to Automatically Use the Product SKU as the Focus Keyword

#559628
Viewing 12 replies - 1 through 12 (of 12 total)
  • I have tried to use $product->getsku(); and it didn’t work either

    Hello,

    Thank you for contacting us and bringing your concern to our attention.

    Please try to use the following filter on your site and see if that helps you to use your product SKU as the product focus keyword:

    function update_product_focus_keywords()
    {
        $products = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'product' //replace post with the name of your post type
        ));
        foreach ($products as $p) {
            $sku = get_post_meta($p->ID, '_sku', true);
            if (!empty($sku)) {
                update_post_meta($p->ID, 'rank_math_focus_keyword', $sku);
            }
        }
    }
    add_action('init', 'update_product_focus_keywords');
    

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Hello,

    Thanks for the reply

    but this caused a fatal error in both backend and frontend.

    Hello,

    The error is caused by the 'posts_per_page' => -1 array value, it will cause an out of memory error.
    I have changed it to 100 and it only changed one product keyword

    Please note that i have 700k product

    Hello,

    Apologies for the inconvenience.

    Yes, this line reads/processes all your posts, which can slow down your site and throw the error:
    'posts_per_page' => -1

    However, you can limit the number of posts that you process each time by setting a limit on the posts like so:

    'posts_per_page' => 100
    

    You can then add an offset to the function as well to continue adding the focus keywords to newer posts. So, after changing the first 100 posts you add an offset of 100 to the function like so:

    'offset' => 100
    

    You can continue to increase the offset by 100 until it runs through all of the posts.

    Each time you run the function, you can add 100 to the offset value like,

    1st time : 'offset' => 0, 
    2nd time : 'offset' => 100,
    3rd time : 'offset' => 200,

    And so on…

    Hope that helps.

    Thank you.

    Hello,

    Thanks ofr the explanation.

    is there a way to use it as cron job?, because as i said, i have 700k product and it will take me forever to finish it this way.

    Thanks

    i’m using the snippet as following but it only update the first product in products page !

    function update_product_focus_keywords()
    {
        $products = get_posts(array(
            'posts_per_page' => 10000,
    		'offset' => 0,
            'post_type' => 'product' //replace post with the name of your post type
        ));
        foreach ($products as $p) {
            $sku = get_post_meta($p->ID, '_sku', true);
            if (!empty($sku)) {
                update_post_meta($p->ID, 'rank_math_focus_keyword', $sku);
            }
        }
    }
    add_action('init', 'update_product_focus_keywords');

    Hello,

    The code worked fine 🙂

    How can the focus keyword be added to any new product i create ?

    and if you can add all that snippets to tools, just like the recalculate SEO scores

    Thanks a lot for the great plugin

    Nigel
    Rank Math business

    Hello,

    Please remove the previous code snippet as it is meant to update the keyword for bulk existing products, so it needs to only run once. You can use code snippet below to set the sku to focus keyword whenever a new/existing product is saved/updated:

    
    function set_current_product_focus_keyword(){
    	global $post; 
    	if (get_post_type() != 'product'){
    		return;
    	}
    	$sku = get_post_meta($post->ID, '_sku', true);
    	if (!empty($sku)) {
    		update_post_meta($post->ID, 'rank_math_focus_keyword', $sku);
    	}
    }
    add_action( 'save_post', 'set_current_product_focus_keyword' );
    add_action( 'edit_post', 'set_current_product_focus_keyword' );
    

    Hope that helps. Please let us know if you have questions.

    Thanks a lot for all the support

    Hello,

    Glad that helped.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help.

    Thank you.

Viewing 12 replies - 1 through 12 (of 12 total)

The ticket ‘How to Automatically Use the Product SKU as the Focus Keyword’ is closed to new replies.