Auto setup focus keyword for products if these have no focus keywords before

#247298
  • Hello, I have just imprort ~2.000 products from my software to wordpress.
    Little of theme have forcus keywords, but alot of theme have no keywords.
    What I want is a code, can help me auto setup focus keyword for products if these have no focus keywords before (name of product as forcus keyword).
    I try to us this code:

    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){
    if(get_the_tags($p->ID)){
    foreach((get_the_tags($p->ID)) as $tag) {
    $keywords[] = strtolower($tag->name);
    }
    update_post_meta($p->ID,'rank_math_focus_keyword',implode(", ", array_unique($keywords)));
    }
    }

    }
    add_action( 'init', 'update_focus_keywords' );

    And add it to file: function.php but it not word, additinal I still do not check traddition “if have no forcus before”.
    Can you help me?

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

    Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.

    That code will only work if the products have tags associated with them and would insert those tags in the focus keywords field for all of them.

    If your products don’t have tags you might want to use the title with something like this:

    
    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){
    		update_post_meta($p->ID,'rank_math_focus_keyword',strtolower(get_the_title($p->ID)));
    		
    	}
    }
    add_action( 'init', 'update_focus_keywords' );
    

    ​​​​​​​Hope this helps solve your issues.

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

    Thank you!
    What about check have focus keyword or not?

    Hello,

    I modified the filter a bit to achieve that. Please use this filter code instead:

    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){
    			if(!get_post_meta($p->ID, 'rank_math_focus_keyword', true)){
    		        update_post_meta($p->ID,'rank_math_focus_keyword',strtolower(get_the_title($p->ID)));
    			}
    	}
    }
    add_action( 'init', 'update_focus_keywords' );

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

    Thank you so much. I have just do that code write by Jeremy.
    It work great! All Point of SEO improve from 3-7 to over than 50.

    Hello,

    You’re welcome.

    We are super happy that this issue is already resolved. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind us asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

The ticket ‘Auto setup focus keyword for products if these have no focus keywords before’ is closed to new replies.