Custom Fields
Re-Envisioned
Support MB Frontend Submission How to Autofill the Title of the cutstom post with the value of the 2 fields

  • Creator
    Topic
  • #27163
    Resolved Ulysse
    Participant

    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.

Viewing 10 replies - 1 through 10 (of 11 total)
  • Author
    Replies
  • #27169
    Long Nguyen
    Moderator

    Hi,

    Please follow this topic https://metabox.io/support/topic/adding-metabox-field-data-to-the-post-title/ to know how to update the post title by the field value.

    #27172
    Ulysse
    Participant

    Hello,
    I use the MB builder.
    Your tuto does not use it.
    Is there a solution with the MB builder?

    #27180
    Long Nguyen
    Moderator

    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?

    #27188
    Ulysse
    Participant

    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).

    #27271
    Ulysse
    Participant

    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?

    #27276
    Long Nguyen
    Moderator

    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.

    #27290
    Ulysse
    Participant

    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;
    }
    
    #27291
    Ulysse
    Participant

    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 );
    }
    
    #27292
    Ulysse
    Participant

    When the snippet is activated:

    • WHen I save a post, the title becomes empty (no title) and the slug url (post_name) becomes "_"
    • So it means that the snippet does something. And because the post_name only becomes "_", it means that $t1 and $t2 are empty.
      But I don't know why.
    #27293
    Ulysse
    Participant

    Here is a screenshot of my snippet: https://prnt.sc/11gopgk

Viewing 10 replies - 1 through 10 (of 11 total)
  • You must be logged in to reply to this topic.