Support Forum
Support › MB Frontend Submission › How to Autofill the Title of the cutstom post with the value of the 2 fieldsResolved
Hello,
I created a custom post named "Agent" associated to custom fields.
In the form, there is the custom fields first_name_agent and last_name_agent
How can I fill the title of the post with first_name_agent + last_name_agent? I tried with custom settings but failed.
For example, the user fills: John as first name and Ford and as alst name, then the title of the post would be John Ford.
Thank you.
Hi,
Please follow this topic https://support.metabox.io/topic/adding-metabox-field-data-to-the-post-title/ to know how to update the post title by the field value.
Hello,
I use the MB builder.
Your tuto does not use it.
Is there a solution with the MB builder?
Hi,
The Builder does the same as the code, you can click on the button Generate PHP code to see how it works. See more here https://docs.metabox.io/extensions/meta-box-builder/#getting-php-code.
Did you try to use the custom code?
Honnestly, as A build my custom fields with the BM builder, I don't see how to manage the code.
Can I add the code to the function.php your provided and keep the BM Builder?
(I don't want to put everything in the function.php. I want to keep the flexibility/ease of use of the MB builder).
(sorry to be rooky in that domain).
Hello,
I have created this snippet (I don't want to change my function.php) with code snippet:
my metabox is fields-for-agent
add_action( 'rwmb_fields-for-agent_after_save_post', 'update_post_title' );
function update_post_title( $post_id ) {
// Get the field value
$t1 = rwmb_meta( 'first_name', '', $post_id );
$t2 = rwmb_meta( 'last_name', '', $post_id );
// Get the post title
$my_post_title = get_the_title( $post_id );
// Preprare update post
$my_post = array(
'ID' => $post_id,
'post_title' => $t1 . ' ' . $t2,
'post_name' => $t1 . '_' . $t2,
);
wp_update_post( $my_post );
}
But it does not work and I don't find why.
Maybe because the field "post_title" is in the metabox?
Hi,
The code works well on my local site. Can you please share the code that creates the meta box and custom fields? I will help you to check it.
Thank you.
Here are the custom fields:
<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Fields for agent', 'your-text-domain' ),
'id' => 'fields-for-agent',
'post_types' => ['agent'],
'tab_style' => 'box',
'tab_default_active' => 'identity',
'geo' => [
'api_key' => 'AIzaSyDdkC7vnU_nm-feorsOVR6FUWG_MCW3zzg',
],
'tabs' => [
'identity' => [
'label' => 'Identity',
'icon' => 'admin-users',
],
'location_and_languages' => [
'label' => 'Location and Languages',
'icon' => 'admin-site',
],
'your_offer' => [
'label' => 'Your offer',
'icon' => 'welcome-edit-page',
],
],
'fields' => [
[
'name' => __( 'First name', 'your-text-domain' ),
'id' => $prefix . 'first_name_agent',
'type' => 'text',
'admin_columns' => [
'position' => 'after',
'sort' => true,
'searchable' => true,
'filterable' => true,
],
'columns' => 6,
'visible' => [
'when' => [['', '=', '']],
'relation' => 'or',
],
'tab' => 'identity',
],
[
'name' => __( 'Last name', 'your-text-domain' ),
'id' => $prefix . 'last_name_agent',
'type' => 'text',
'columns' => 6,
'tab' => 'identity',
],
[
'name' => __( 'Photo', 'your-text-domain' ),
'id' => $prefix . '_thumbnail_id',
'type' => 'image',
'label_description' => __( 'Photo which best promotes you', 'your-text-domain' ),
'max_file_uploads' => 1,
'admin_columns' => [
'position' => 'before post_title',
'searchable' => true,
],
'tab' => 'identity',
],
[
'name' => __( 'Company', 'your-text-domain' ),
'id' => $prefix . 'company_agent',
'type' => 'text',
'columns' => 6,
'tab' => 'identity',
],
[
'name' => __( 'Website', 'your-text-domain' ),
'id' => $prefix . 'website_agent',
'type' => 'url',
'columns' => 6,
'tab' => 'identity',
],
[
'name' => __( 'Email', 'your-text-domain' ),
'id' => $prefix . 'email_agent',
'type' => 'email',
'columns' => 6,
'tab' => 'identity',
],
[
'name' => __( 'Tel', 'your-text-domain' ),
'id' => $prefix . 'tel_agent',
'type' => 'tel',
'columns' => 6,
'tab' => 'identity',
],
[
'name' => __( 'Address', 'your-text-domain' ),
'id' => $prefix . 'address_agent',
'type' => 'text',
'tab' => 'location_and_languages',
],
[
'name' => __( 'Country', 'your-text-domain' ),
'id' => $prefix . 'country_agent',
'type' => 'text',
'binding' => 'country',
'address_field' => 'address_agent',
'tab' => 'location_and_languages',
],
[
'name' => __( 'Map', 'your-text-domain' ),
'id' => $prefix . 'map_gm_agent',
'type' => 'map',
'api_key' => 'AIzaSyDdkC7vnU_nm-feorsOVR6FUWG_MCW3zzg',
'address_field' => 'address_agent',
'tab' => 'location_and_languages',
],
[
'name' => __( 'Language(s)', 'your-text-domain' ),
'id' => $prefix . 'languages',
'type' => 'taxonomy',
'taxonomy' => ['languages'],
'field_type' => 'select_advanced',
'add_new' => true,
'tab' => 'location_and_languages',
],
[
'name' => __( 'Countries of Operation', 'your-text-domain' ),
'id' => $prefix . 'countries_of_operation',
'type' => 'taxonomy',
'taxonomy' => ['country-of-operation'],
'field_type' => 'select_advanced',
'multiple' => true,
'add_new' => true,
'tab' => 'location_and_languages',
],
[
'name' => __( 'Background', 'your-text-domain' ),
'id' => $prefix . 'background_agent',
'type' => 'textarea',
'rows' => 10,
'tab' => 'your_offer',
],
[
'name' => __( 'Services', 'your-text-domain' ),
'id' => $prefix . 'services_agent',
'type' => 'textarea',
'rows' => 10,
'tab' => 'your_offer',
],
[
'name' => __( 'References', 'your-text-domain' ),
'id' => $prefix . 'references',
'type' => 'textarea',
'rows' => 10,
'tab' => 'your_offer',
],
[
'name' => __( 'Video Presentation', 'your-text-domain' ),
'id' => $prefix . 'video_presentation_agent',
'type' => 'url',
'std' => 'https://www.youtube.com/watch?v=ZRngEOnHnoc',
'tab' => 'your_offer',
],
],
];
return $meta_boxes;
}
Here is the custom post:
<?php
add_action( 'init', 'your_prefix_register_post_type' );
function your_prefix_register_post_type() {
$labels = [
'name' => esc_html__( 'Agents', 'your-textdomain' ),
'singular_name' => esc_html__( 'Agent', 'your-textdomain' ),
'add_new' => esc_html__( 'Add New', 'your-textdomain' ),
'add_new_item' => esc_html__( 'Add new agent', 'your-textdomain' ),
'edit_item' => esc_html__( 'Edit Agent', 'your-textdomain' ),
'new_item' => esc_html__( 'New Agent', 'your-textdomain' ),
'view_item' => esc_html__( 'View Agent', 'your-textdomain' ),
'view_items' => esc_html__( 'View Agents', 'your-textdomain' ),
'search_items' => esc_html__( 'Search Agents', 'your-textdomain' ),
'not_found' => esc_html__( 'No agents found', 'your-textdomain' ),
'not_found_in_trash' => esc_html__( 'No agents found in Trash', 'your-textdomain' ),
'parent_item_colon' => esc_html__( 'Parent Agent:', 'your-textdomain' ),
'all_items' => esc_html__( 'All Agents', 'your-textdomain' ),
'archives' => esc_html__( 'Agent Archives', 'your-textdomain' ),
'attributes' => esc_html__( 'Agent Attributes', 'your-textdomain' ),
'insert_into_item' => esc_html__( 'Insert into agent', 'your-textdomain' ),
'uploaded_to_this_item' => esc_html__( 'Uploaded to this agent', 'your-textdomain' ),
'featured_image' => esc_html__( 'Featured image', 'your-textdomain' ),
'set_featured_image' => esc_html__( 'Set featured image', 'your-textdomain' ),
'remove_featured_image' => esc_html__( 'Remove featured image', 'your-textdomain' ),
'use_featured_image' => esc_html__( 'Use as featured image', 'your-textdomain' ),
'menu_name' => esc_html__( 'Agents', 'your-textdomain' ),
'filter_items_list' => esc_html__( 'Filter agents list', 'your-textdomain' ),
'items_list_navigation' => esc_html__( 'Agents list navigation', 'your-textdomain' ),
'items_list' => esc_html__( 'Agents list', 'your-textdomain' ),
'item_published' => esc_html__( 'Agent published', 'your-textdomain' ),
'item_published_privately' => esc_html__( 'Agent published privately', 'your-textdomain' ),
'item_reverted_to_draft' => esc_html__( 'Agent reverted to draft', 'your-textdomain' ),
'item_scheduled' => esc_html__( 'Agent scheduled', 'your-textdomain' ),
'item_updated' => esc_html__( 'Agent updated', 'your-textdomain' ),
'text_domain' => esc_html__( 'your-textdomain', 'your-textdomain' ),
];
$args = [
'label' => esc_html__( 'Agents', 'your-textdomain' ),
'labels' => $labels,
'description' => '',
'public' => true,
'hierarchical' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'show_in_rest' => true,
'menu_position' => 20,
'query_var' => true,
'can_export' => true,
'delete_with_user' => true,
'has_archive' => true,
'rest_base' => '',
'show_in_menu' => true,
'menu_icon' => 'dashicons-admin-generic',
'capability_type' => 'post',
'supports' => ['title', 'editor', 'thumbnail', 'custom-fields', 'revisions', 'author', 'post-formats', 'excerpt', 'trackbacks', 'comments'],
'taxonomies' => ['country-of-operation', 'location-of-agent', 'languages'],
'rewrite' => [
'with_front' => false,
],
];
register_post_type( 'agent', $args );
}
When the snippet is activated:
Here is a screenshot of my snippet: https://prnt.sc/11gopgk
I found it by looking at the code!
in the snippet, I put last_name as variable instead of last_name_agent, the same with first_name.
It works! Thank you for your support!