Can I set bulk keywords?

#223066
  • Resolved PricePacific com
    Rank Math free

    Hello RankMath!!
    I am using woocommerce website. My website has a 22000 products and I am working alone on my website. Out of 20000 per day I can set keyword to 10/12 products. Is there any way to insert keyword to all product page at once?
    My site: https://pricepacific.com

Viewing 15 replies - 1 through 15 (of 25 total)
  • Prabhat
    Rank Math agency

    Hello,

    Thank you for contacting us.

    You can add the below code in your theme’s functions.php file to automatically use the product title as the focus keywords for all products:

    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' );

    For more details, please refer to this tutorial guide:
    https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/

    Hope that helps.

    Thank you.

    Hello!! Thank you for your response. One more question, It will also add focus keyword to old products? Which already posted? And what about the products where I set manual keywords? Do this process override them? Or those keywords remain as it is?

    Got it!! Understood the code!! Thanks!!

    Prabhat
    Rank Math agency

    Hello,

    The code that I’ve provided, would add focus keywords to all your products regardless of how old they are.

    The focus keywords would be added only to those products which are missing them. For the products where you’ve manually added the keywords, no changes would be made.

    Hope that helps.

    Thank you.

    When I add this code. I got this error.!!

    Your PHP code changes were rolled back due to an error on line 2022 of file wp-includes/wp-db.php. Please fix and try saving again.
    Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes

    Ankit
    Rank Math business

    Hello,

    Please get in touch with your web host and ask them to increase the php memory limit on your website.

    Once they do, this issue will go away on its own.

    Hope that helps. Thank you and please don’t hesitate to contact us if you need further assistance.

    Hello!! They Increased PHP memory size. still, I got another error which is below.
    “Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.”

    Okay!! I added code from Cpanel. But now my website is not loading. 503 error. is it temporary? when I write pricepacific.com it will show error like 503 server is temporary busy try again later.

    Hello,

    You are processing 22,000 posts which is a lot. The code my colleague provided will loop all of them and will surely use all your server resources thus creating this 5xx server issue.

    You may need to adjust the code and run it by batch so you won’t use up all your server hardware. You may refer to this ticket here: https://support.rankmath.com/ticket/issue-with-automating-focus-keywords/

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

    function update_focus_keywords() {
        $posts = get_posts(array(
        'posts_per_page'	=> 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' );

    This code will work for me?

    Hello,

    This version of the filter will only process 100 products so that shouldn’t run into any memory issues and it’s probably going to correctly run the function, however, only after trying to run that you’ll know for sure if the memory limit allows for at least 100 products to be processed.

    The issue here is with a limitation on your server resources as processing over 22k products will consume a lot of memory.

    Hope this helps clarify your doubts.

    Don’t hesitate to get in touch if you have any questions.

    Above code is correct? Should put that code in to functions.php? After completing 100 products do I need to put code again?

    Hello,

    Yes, you need to insert that code in your functions.php file.

    Since it will only retrieve the first 100 (or you can make it higher if you want as long as your server can handle it), you may need to set an offset value every time you execute the code. Please see this reply here (#2) explaining how to do it. https://support.rankmath.com/ticket/issue-with-automating-focus-keywords/?view=all#post-197130

    This would require you to repeat this process until all 22k products are processed.

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

    function update_focus_keywords() {
        $posts = get_posts(array(
        'posts_per_page'	=> 100,
        'post_type'		=> 'product' 
        'offset'          => 10  // 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' );

    I have added the offset now how can we loop this code? I am not technical person.

    Hello,

    Since you are limiting the post_per_page to 100, you should also set the offset by hundreds. This should allow you to process the next 100 posts and so on.

    Hope that helps.

    Don’t hesitate to get in touch if you have any questions.

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

The ticket ‘Can I set bulk keywords?’ is closed to new replies.