Automatic generate focus kyeword

#153543
  • Resolved Noor Alam
    Rank Math free

    Hello support,
    I have a website name themeplug.net. My website is a affiliate web site. This site i use a plugin for autoblogging which plugin name wordpress automatic. This plugin do automatic generate post or product from buyer website and automaticly publish it in my website. Is it possibble to setup focus keyword automaticly generate from title or category or tag? If yes, how?

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

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

    I am sorry but Rank math doesn’t have a feature to automate this. You can however modify the following filter to get the title of the post then assign it as the keyword for your posts:

    /**
     * Allow changing the meta keywords from the default Focus Keywords.
     *
     * @param string $keywords Keywords.
     */
    add_filter( 'rank_math/frontend/keywords', function( $keywords ) {
     return $keywords;
    });
    

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

    which file i added this line for generate auto focus keyword from title.

    Anas
    Rank Math business

    Hello,

    You need to add the filter in the functions.php file of your theme.

    I hope this helps. Looking forward to helping you.

    I use rehub theme. i added this code in my child theme function.php see the screenshot https://prnt.sc/104z1ha
    i i publish a post but i do not see any focus keyword automatic generate. see the screenshot https://prnt.sc/104z3hj

    Hello,

    Thank you for the follow-up.

    It seems that you have pasted the filter without customization. You will need to customize the filter to automatically update your post’s keywords.

    Here is a sample filter working on our end to get the post’s tag and add it to your keywords:

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

    Please note that if it’s not working on your end, you will need to customize it based on your site requirements. Unfortunately, Rank Math really doesn’t have this kind of feature so you need to create a WordPress functionality to achieve this.

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

    where i past this code? which file i added this code? is it working post and also woo commerce product page?

    Hello,

    Thank you for keeping in touch with us.

    You will need to add the code in your theme’s functions.php file (towards the very bottom) for this custom feature to work.

    Please see the video screencast below:
    https://i.rankmath.com/sbl7Sk

    Note: If you constantly updating your theme, you might need to create a child theme for this one.

    If you need to use this function code for your products, you can simply change the post_type value from post to product within the code.

    If you are not sure, let us know so we can help you.

    Thank you.

    i using your this code in my child theme function.php when i added this code in function.php at the end of code. my server is down, and show this in my cpanal phusical memory uses and iops is full . see the screenshot https://prnt.sc/105g0tq

    Hello,

    Can you please let me know how many posts/pages/products you are trying to process? If you have a ton of CPT’s to process using this code, it might lead to server load issues considering you only have 1GB of memory available.

    This code will retrieve all your CPT’s and will add focus keywords in a looping method which will surely use a lot of CPU/Memory usage especially if you have a lot of CPT’s.

    If you need this custom feature on your website, you might need to consider upgrading your server.

    Furthermore, I am submitting this issue to our developers to see if they have any suggestions.

    For now, please remove the code so your users will not be affected when accessing your website.

    Thank you.

    Hello,

    We wanted to add that you can change the code in the filter we offered to limit the number of posts processed.
    Please change this:
    'posts_per_page' => -1,

    To This:
    'posts_per_page' => 10,

    That will change the number of posts processed from unlimited posts to just 10.

    Hope that helps.

    Thank you so much. this code now working properly. but this code does not working for both Type product & Post at the same time. if i write post type post it will work post and when i change post type product it will work product. but i want to it work both post type “product and post” at the same time.

    and another problem it will generate automatically focus keyword from tag, but i want to setup automatically focus keyword from category, or title or #tag

    Hello,

    I am afraid to do this for both posts and products you may need to have two functions as these two are two different post types.

    To use the categories, you can use the filter as follows:

    
    function update_focus_keywords() {
    	$posts = get_posts(array(
    	'posts_per_page'	=> -1,
    	'post_type'		=> 'post' //replace post with the name of your post type
        ));
    	foreach($posts as $p){
    		if(get_the_category($p->ID)){
    			foreach((get_the_category($p->ID)) as $category) {
                    $keywords[] = strtolower($category->name);
                }
    		update_post_meta($p->ID,'rank_math_focus_keyword',implode(", ", array_unique($keywords)));
    		}
    	}
        
    }
    add_action( 'init', 'update_focus_keywords' );
    

    Hope this helps you

    I added this code in my child theme for both “post and product” but it do not work for both.

    function update_focus_keywords() {
    	$posts = get_posts(array(
    	'posts_per_page'	=> 30,
    	'post_type'		=> 'post' //replace post with the name of your post type
        ));
    	foreach($posts as $p){
    		if(get_the_category($p->ID)){
    			foreach((get_the_category($p->ID)) as $category) {
                    $keywords[] = strtolower($category->name);
                }
    		update_post_meta($p->ID,'rank_math_focus_keyword',implode(", ", array_unique($keywords)));
    		}
    	}
        
    }
    add_action( 'init', 'update_focus_keywords' );
    function update_focus_keywords() {
    	$posts = get_posts(array(
    	'posts_per_page'	=> 30,
    	'post_type'		=> 'product' //replace post with the name of your post type
        ));
    	foreach($posts as $p){
    		if(get_the_category($p->ID)){
    			foreach((get_the_category($p->ID)) as $category) {
                    $keywords[] = strtolower($category->name);
                }
    		update_post_meta($p->ID,'rank_math_focus_keyword',implode(", ", array_unique($keywords)));
    		}
    	}
        
    }
    add_action( 'init', 'update_focus_keywords' );

    Hello,

    Thank you for keeping in touch with us.

    You are declaring duplicate functions which will surely lead to an error. Please make the function names unique. You can use this code instead:

    function update_focus_keywords() {
    	$posts = get_posts(array(
    		'posts_per_page'	=> 30,
    		'post_type'		=> 'post' //replace post with the name of your post type
        ));
    	foreach($posts as $p){
    		if(get_the_category($p->ID)){
    			foreach((get_the_category($p->ID)) as $category) {
                    $keywords[] = strtolower($category->name);
                }
    			update_post_meta($p->ID,'rank_math_focus_keyword',implode(", ", array_unique($keywords)));
    		}
    	}
    }
    add_action( 'init', 'update_focus_keywords' );
    
    function update_focus_keywords1() {
    	$posts = get_posts(array(
    		'posts_per_page'	=> 30,
    		'post_type'		=> 'product' //replace post with the name of your post type
        ));
    	foreach($posts as $p){
    		if(get_the_category($p->ID)){
    			foreach((get_the_category($p->ID)) as $category) {
                    $keywords[] = strtolower($category->name);
                }
    			update_post_meta($p->ID,'rank_math_focus_keyword',implode(", ", array_unique($keywords)));
    		}
    	}
    }
    add_action( 'init', 'update_focus_keywords1' );

    Let me know if this works on your end.

    Noor Alam
    Rank Math free

    thank you for this solution

Viewing 15 replies - 1 through 15 (of 16 total)

The ticket ‘Automatic generate focus kyeword’ is closed to new replies.