Automatic focus keyword for a custom post type

#701899
  • Resolved Timverzalen
    Rank Math free

    I have a custom post type with houses, I want to automatically add focus keywords (title as focus keyword (without – businessname). Is this possible to do only for the post type ‘Woningen’?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello, I’m sorry for the inconvenience you’re facing. To automatically add the title as the focus keyword for your custom post type ‘Woningen’, you can use the following code snippet in your theme’s functions.php file:

    /**
     * Function to automatically update the focus keyword with the post title for custom post type 'Woningen'
     */
    function update_focus_keywords_for_custom_post_type() {
        $posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'woningen' // Replace 'woningen' with the name of your custom 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_for_custom_post_type');

    Please ensure that you replace 'woningen' with the actual name of your custom post type.

    This code will automatically set the post title as the focus keyword for the custom post type ‘Woningen’. 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

    Timverzalen
    Rank Math free

    I did put this in my functions php but no luck:

    /**
    * Function to automatically update the focus keyword with the post title, if no focus keyword is set
    */
    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){
    // 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_focus_keywords’ );

    Hello,

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

    From your last response, it appears you didn’t modify the post type in the code to reflect the custom post type.

    If woningen is the custom post type then it should be 'post_type' => 'woningen'

    Let us know how this goes. Please let us know if you have further questions or concerns.

    Thank you.

    Timverzalen
    Rank Math free

    I tried this:
    But it did not work (on my staging site)

    /**
    * Function to automatically update the focus keyword with the post title, if no focus keyword is set
    */
    function update_focus_keywords() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘woningen’ // 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_focus_keywords’ );

    Hello,

    To help you investigate this issue, we might need to take a closer look at your website and settings. Please follow these steps:

    • Edit the first post on this ticket and include your WordPress and FTP logins in the Sensitive Data section. This is a secure section that only our support staff can access.
      Sensitive Data Section
    • Make sure you take a complete backup of your website before sharing the information with us.
    • If you prefer, you can use this plugin to generate a temporary login URL to your website and share that with us instead: https://wordpress.org/plugins/temporary-login-without-password/
    • You can also use this plugin to monitor what changes our staff might make on your website (if any): https://wordpress.org/plugins/wp-security-audit-log/

    We appreciate your cooperation and patience. Thank you for choosing Rank Math.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘Automatic focus keyword for a custom post type’ is closed to new replies.