Automatically add site title and custom taxonomy as keywords

#700250
  • Resolved Anonymous
    Rank Math free

    Hello everyone. To be honest, this is not a support question per se. Is more of a “help me if you can” one.

    Following this guide I’ve been able to automate the insertion of the site title as focus keyword to a custom post type named podcast, if no focus keyword has been set.

    What I am trying to achieve is to add to the focus keywords also a custom taxonomy I’ve created for poodcast CPT called categorie-podcast.

    I’m using ACF to create both the CPT and the Custom Taxonomies associated to it.

    The code I have right now is


    /**
    * Function to automatically update the RankMath focus keyword of "podcast" cpt with the site title, if no focus keyword is set
    */
    function update_site_title_focus_keywords() {
    $posts = get_posts(array(
    'posts_per_page' => -1,
    'post_type' => 'podcast' // Only for post type "podcast"
    ));

    // Get the site title
    $site_title = get_bloginfo('name');

    foreach ($posts as $p) {
    // Check if Rank Math keyword already exists and only update if it doesn't have it
    $rank_math_keyword = get_post_meta($p->ID, 'rank_math_focus_keyword', true);
    if (!$rank_math_keyword) {
    // Set the focus keyword to the site title
    update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower($site_title));
    }
    }
    }

    add_action('init', 'update_site_title_focus_keywords');

    Any help would be appreciated

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello, I understand that you’ve been able to automate the insertion of the site title as the focus keyword for a custom post type named “podcast” using the guide provided. You’re now looking to add a custom taxonomy called “categorie-podcast” to the focus keywords as well.

    To achieve this, you can modify the existing code to include the custom taxonomy. Here’s an example of how you can do this:

    /**
     * Function to automatically update the RankMath focus keyword of "podcast" CPT with the site title and custom taxonomy, if no focus keyword is set
     */
    function update_site_title_and_taxonomy_focus_keywords() {
        $posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'podcast' // Only for post type "podcast"
        ));
    
        // Get the site title
        $site_title = get_bloginfo('name');
    
        foreach ($posts as $p) {
            // Check if Rank Math keyword already exists and only update 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 custom taxonomy terms
                $terms = get_the_terms($p->ID, 'categorie-podcast');
                $taxonomy_keywords = [];
                if ($terms) {
                    foreach ($terms as $term) {
                        $taxonomy_keywords[] = strtolower($term->name);
                    }
                }
                // Set the focus keyword to the site title and custom taxonomy terms
                $keywords = array_merge([$site_title], $taxonomy_keywords);
                update_post_meta($p->ID, 'rank_math_focus_keyword', implode(", ", array_unique($keywords)));
            }
        }
    }
    
    add_action('init', 'update_site_title_and_taxonomy_focus_keywords');

    This modified code will fetch the custom taxonomy terms associated with each podcast post and add them to the focus keywords along with the site title.

    I hope this helps! If you have any further questions or need additional assistance, feel free to ask.

    Sources:
    How to Automate the Insertion of Focus Keyword » Rank Math

    Anonymous
    Rank Math free

    RankBot you must become king of the world!
    Thank you.

    Hello,

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

    If you don’t mind me 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 do 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 3 replies - 1 through 3 (of 3 total)

The ticket ‘Automatically add site title and custom taxonomy as keywords’ is closed to new replies.