Urgent Help Needed!

#583135
  • Resolved Anonymous
    Rank Math free

    Hi Team,

    We would like to know how we can insert the PAGE TITLE as the focus keyword on all our PAGES* automatically. We have over 2k pages and this will be a very big task for us if done manually. Any help will be great.

    Regards,
    Michael

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

    Thanks for contacting Rank Math support.

    I understand that you have over 2k pages and setting a focus keyword for each one manually can be a lot of work. Don’t worry, I have a solution for you. You can use some simple code to do this automatically.

    All you need to do is copy and paste one of the code snippets below into your theme’s functions.php or rank-math.php file. The code will run through all your pages and set the page title as the focus keyword.

    The first code snippet will replace any focus keywords that you have already set manually. Use this if you want to start fresh and use only the page titles as your focus keywords.

    The second code snippet will check if a page has a focus keyword already and leave it alone. Use this if you want to keep your existing focus keywords and only update the pages that don’t have one.

    Here are the code snippets:

    // Automatically set the page title as the focus keyword
    function update_focus_keywords() {
      $posts = get_posts( array(
        'posts_per_page' => -1,
        'post_type' => 'page' //replace page with the name of your post type
      ));
      foreach ($posts as $p) {
        // Get the post title
        $post_title = get_the_title( $p->ID );
        // Set the post title as the focus keyword
        update_post_meta( $p->ID, 'rank_math_focus_keyword', $post_title );
      }
    }
    add_action( 'init', 'update_focus_keywords' );
    // Automatically set the page title as the focus keyword, if no focus keyword is set
    function update_focus_keywords() {
      $posts = get_posts( array(
        'posts_per_page' => -1,
        'post_type' => 'page' //replace page 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 ) {
    		// Get the post title
    		$post_title = get_the_title( $p->ID );
    		// Set the post title as the focus keyword
    		update_post_meta( $p->ID, 'rank_math_focus_keyword', $post_title );
    	}
      }
    }
    add_action( 'init', 'update_focus_keywords' );

    I hope this helps you achieve what you want. If you have any questions or need more information, please let me know. I’m always here to help.

    Thank you for using Rank Math.

    Anonymous
    Rank Math free

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Anonymous
    Rank Math free

    UPDATE****

    Hi,

    We have resolved this as follows:

    To fix all issue relating to the previous reply:

    We needed to change the (-1) to 100 and add an offset line under that. as follows:

    // Automatically set the page title as the focus keyword
    function update_focus_keywords() {
    $posts = get_posts( array(
    ‘posts_per_page’ => 100,
    ‘offset’ => 2000,
    ‘post_type’ => ‘page’ //replace page with the name of your post type
    ));
    foreach ($posts as $p) {
    // Get the post title
    $post_title = get_the_title( $p->ID );
    // Set the post title as the focus keyword
    update_post_meta( $p->ID, ‘rank_math_focus_keyword’, $post_title );
    }
    }
    add_action( ‘init’, ‘update_focus_keywords’ );

    We needed to repeat this process 20 times before all our pages focus keyword can be successfully added.

    More information on this can be found on
    https://support.rankmath.com/ticket/automate-the-insertion-of-focus-keyword-9/

    Thank You.

    Hello again,

    We are glad to hear that this issue has been resolved. Thank you for letting us know. This ticket will be closed now, but you can always open a new one if you have any other questions or concerns. We are here to help you with anything related to Rank Math.

    We appreciate your patience and cooperation throughout this process.

    Thank you for choosing Rank Math.

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

The ticket ‘Urgent Help Needed!’ is closed to new replies.