custom post type Focus Keyword

#355204
  • Resolved Liuyulin
    Rank Math free

    How to customize the text type label to automatically set the label title as the focus keyword?

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

    Thanks for contacting us and sorry for any inconvenience that might have been caused due to that.

    Can you please confirm if you have a custom post type named ‘Label’ and you want to set its title as the focus keyword? If yes then please use the code given below in your theme’s functions.php file:

    
    /**
     * Function to automatically update the focus keyword with the post title
     */
    function update_focus_keywords()
    {
        $posts = get_posts(array(
            'posts_per_page'    => -1,
            'post_type'        => 'label' //replace post with the name of your 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');
    

    Let us know if this works or please share some more details about your custom post type’s setup.

    Looking forward to helping you.

    The name of the custom text type is product, and it has the attribute of label.

    add_action( 'init', 'codex_product_init' );
    function codex_product_init() {
    $labels = array(
    'name' => _x( 'Product', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name' => _x( 'Product', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name' => _x( 'Product', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar' => _x( 'Product', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new' => _x( 'Add Product', 'product', 'your-plugin-textdomain' ),
    'add_new_item' => __( 'Add Product', 'your-plugin-textdomain' ),
    'new_item' => __( 'New Product', 'your-plugin-textdomain' ),
    'edit_item' => __( 'Edit Product', 'your-plugin-textdomain' ),
    'view_item' => __( 'View Product', 'your-plugin-textdomain' ),
    'all_items' => __( 'All Product', 'your-plugin-textdomain' ),
    'search_items' => __( 'Search Product', 'your-plugin-textdomain' ),
    'parent_item_colon' => __( 'Parent Product:', 'your-plugin-textdomain' ),
    'not_found' => __( 'No Found Product', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No Found Product', 'your-plugin-textdomain' )
    );

    $args = array(
    'labels' => $labels,
    'description' => __( 'Description.', 'your-plugin-textdomain' ),
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'products' ),
    'capability_type' => 'post',
    'has_archive' => false,
    'hierarchical' => false,
    'menu_position' => null,
    'show_in_rest' => true,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'product', $args );
    }
    /** 文章分类功能 ***/
    function codex_product_taxonomies() {
    $labels = array(
    'name' => _x( 'Product Categories', 'taxonomy general name', 'textdomain' ),
    'singular_name' => _x( 'Product Categories', 'taxonomy singular name', 'textdomain' ),
    'search_items' => __( '搜索产品分类', 'textdomain' ),
    'all_items' => __( '所有产品分类', 'textdomain' ),
    'parent_item' => __( '父级分类', 'textdomain' ),
    'parent_item_colon' => __( 'Parent Categories:', 'textdomain' ),
    'edit_item' => __( '编辑产品分类', 'textdomain' ),
    'update_item' => __( '更新产品分类', 'textdomain' ),
    'add_new_item' => __( '添加新产品分类', 'textdomain' ),
    'new_item_name' => __( 'New Categories Name', 'textdomain' ),
    'menu_name' => __( '产品分类', 'textdomain' ),
    );
    $args = array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'show_in_rest' => true,
    'rewrite' => array( 'slug' => 'product-categories' ),
    );
    register_taxonomy( 'product_category', 'product', $args );
    unset( $args );
    unset( $labels );
    $labels = array(
    'name' => _x( 'Product Tags', 'taxonomy general name', 'textdomain' ),
    'singular_name' => _x( 'Tag', 'taxonomy singular name', 'textdomain' ),
    'search_items' => __( 'Search Tags', 'textdomain' ),
    'popular_items' => __( 'Popular Tags', 'textdomain' ),
    'all_items' => __( 'All Tags', 'textdomain' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Tag', 'textdomain' ),
    'update_item' => __( 'Update Tag', 'textdomain' ),
    'add_new_item' => __( 'Add New Tag', 'textdomain' ),
    'new_item_name' => __( 'New Tag Name', 'textdomain' ),
    'separate_items_with_commas' => __( 'Separate tags with commas', 'textdomain' ),
    'add_or_remove_items' => __( 'Add or remove tags', 'textdomain' ),
    'choose_from_most_used' => __( 'Choose from the most used tags', 'textdomain' ),
    'not_found' => __( 'No tags found.', 'textdomain' ),
    'menu_name' => __( '标签', 'textdomain' ),
    );
    $args = array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'show_in_rest' => true,
    'rewrite' => array( 'slug' => 'product-tags' ),
    );
    register_taxonomy( 'product_tag', 'product', $args );
    }
    add_action( 'init', 'codex_product_taxonomies', 0 );

    Automatically set the title setting of product-tags as the focus keyword of the current tag.

    Hello,

    You will have to customize the code to fetch the attribute and use it as the focus keyword.

    Unfortunately, such customizations fall outside the scope of Rank Math support and you would need to hire a developer to implement such code on your website.

    Please don’t hesitate to get in touch in case you need any other assistance.

    Thanks.

    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 ‘custom post type Focus Keyword’ is closed to new replies.