Automating Focus Keyword snippet

#609506
  • Resolved Anonymous
    Rank Math free

    Good day. I have inserted your suggested snippet to automatically add focus keyword based on the title of a custom post (animals). This works fine. However, I would also like to do the same for another CPT (plants).

    What I tried was inserting a second snippet for the plants with new function name.

    Animals snippet:
    /**
    * Function to automatically update the focus keyword with the post title, if no focus keyword is set
    */
    function update_animal_focus_keywords() {
    $posts = get_posts(array(
    'posts_per_page' => -1,
    'post_type' => 'animals' // 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_animal_focus_keywords' );

    Plants snippet:
    /**
    * Function to automatically update the focus keyword with the post title, if no focus keyword is set
    */
    function update_plant_focus_keywords() {
    $posts = get_posts(array(
    'posts_per_page' => -1,
    'post_type' => 'plants' // 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_plant_focus_keywords' );

    This seems to work, but if both snippets are enabled, it messes up the display of my featured image in the side panel. It just doesn’t display. As soon as I turn off either of the 2 snippets, all is well again.

    Perhaps I need to use one snippet that references both post types?

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

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

    This line in the filter reads/processes all your posts, which can slow down your site or even generate errors if you have many posts and pages on your site:

    ‘posts_per_page’ => -1

    If this is affecting the featured image due to PHP memory or resource limitations, 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.

    Anonymous
    Rank Math free

    Got it. Just making sure I was doing it correctly.

    Anonymous
    Rank Math free

    Forgot to say please close the ticket.

    Hello,

    Glad that everything has been sorted out.

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

    We are here to help.

    Thank you.

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

The ticket ‘Automating Focus Keyword snippet’ is closed to new replies.