Custom Fields
Re-Envisioned
Support General custom field as post title

  • Creator
    Topic
  • #28331
    Resolved Qu1nt
    Participant

    I have a CPT called health_forms I want to change the post title to be the custom fields lastname firstname.

    I had it working with just the lastname until I changed it to a custom table.

    The custom fields are being saved in a table wpml_health_form

    How would I go about doing this as I am very new to PHP.

    Thank you

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

    Hi Quint,

    Thank you for reaching out.

    You can follow this topic to know how to replace the post title by custom field value https://metabox.io/support/topic/adding-metabox-field-data-to-the-post-title/

    And this documentation to get the field value in the custom table https://docs.metabox.io/extensions/mb-custom-table/

    Let me know if it helped.

    #28656
    Qu1nt
    Participant

    Hi Long,

    Yeah I'm lost, not sure what I'm doing wrong. Would it be possible for you to show me how to do this?

    Thanks,

    Quint

    #28672
    Long Nguyen
    Moderator

    Hi Quint,

    What is your code that you are trying to do? Here is the example code on your case.

    add_action( 'rwmb_MetaBoxID_after_save_post', 'update_post_title' );
    
    function update_post_title( $post_id ) {
        // Get the field value
        $firstname = rwmb_meta( 'firstname', ['storage_type' => 'custom_table', 'table' => 'wpml_health_form'] );
        $lastname = rwmb_meta( 'lastname', ['storage_type' => 'custom_table', 'table' => 'wpml_health_form'] );
        
        // Preprare update post
        $my_post = array(
            'ID' => $post_id,
            'post_title' => $firstname . ' ' . $lastname,
        );
    
        wp_update_post( $my_post );
    }
    #28725
    Qu1nt
    Participant

    Hi Long,

    It's still not working, still comes up as 'no title'

    #28739
    Long Nguyen
    Moderator

    Hi Quint,

    Can you please share the code that creates the meta box and custom fields?

    #28808
    Qu1nt
    Participant

    Hi Long,

    Below is the code for both.

    <?php
    add_action( 'init', 'your_prefix_register_post_type' );
    function your_prefix_register_post_type() {
        $labels = [
            'name'                     => esc_html__( 'Health Forms', 'your-textdomain' ),
            'singular_name'            => esc_html__( 'Health Form', 'your-textdomain' ),
            'add_new'                  => esc_html__( 'Add New', 'your-textdomain' ),
            'add_new_item'             => esc_html__( 'Add new health form', 'your-textdomain' ),
            'edit_item'                => esc_html__( 'Edit Health Form', 'your-textdomain' ),
            'new_item'                 => esc_html__( 'New Health Form', 'your-textdomain' ),
            'view_item'                => esc_html__( 'View Health Form', 'your-textdomain' ),
            'view_items'               => esc_html__( 'View Health Forms', 'your-textdomain' ),
            'search_items'             => esc_html__( 'Search Health Forms', 'your-textdomain' ),
            'not_found'                => esc_html__( 'No health forms found', 'your-textdomain' ),
            'not_found_in_trash'       => esc_html__( 'No health forms found in Trash', 'your-textdomain' ),
            'parent_item_colon'        => esc_html__( 'Parent Health Form:', 'your-textdomain' ),
            'all_items'                => esc_html__( 'All Health Forms', 'your-textdomain' ),
            'archives'                 => esc_html__( 'Health Form Archives', 'your-textdomain' ),
            'attributes'               => esc_html__( 'Health Form Attributes', 'your-textdomain' ),
            'insert_into_item'         => esc_html__( 'Insert into health form', 'your-textdomain' ),
            'uploaded_to_this_item'    => esc_html__( 'Uploaded to this health form', '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__( 'Health Forms', 'your-textdomain' ),
            'filter_items_list'        => esc_html__( 'Filter health forms list', 'your-textdomain' ),
            'items_list_navigation'    => esc_html__( 'Health forms list navigation', 'your-textdomain' ),
            'items_list'               => esc_html__( 'Health Forms list', 'your-textdomain' ),
            'item_published'           => esc_html__( 'Health Form published', 'your-textdomain' ),
            'item_published_privately' => esc_html__( 'Health form published privately', 'your-textdomain' ),
            'item_reverted_to_draft'   => esc_html__( 'Health form reverted to draft', 'your-textdomain' ),
            'item_scheduled'           => esc_html__( 'Health Form scheduled', 'your-textdomain' ),
            'item_updated'             => esc_html__( 'Health Form updated', 'your-textdomain' ),
            'text_domain'              => esc_html__( 'your-textdomain', 'your-textdomain' ),
        ];
        $args = [
            'label'               => esc_html__( 'Health Forms', 'your-textdomain' ),
            'labels'              => $labels,
            'description'         => '',
            'public'              => true,
            'hierarchical'        => true,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'show_ui'             => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'show_in_rest'        => true,
            'query_var'           => true,
            'can_export'          => true,
            'delete_with_user'    => false,
            'has_archive'         => true,
            'rest_base'           => '',
            'show_in_menu'        => true,
            'menu_icon'           => 'dashicons-clipboard',
            'menu_position'       => 5,
            'capability_type'     => 'post',
            'supports'            => ['title'],
            'taxonomies'          => [],
            'rewrite'             => [
                'with_front' => false,
            ],
        ];
    
    register_post_type( 'health-form', $args );
    
    }
    
    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
    $meta_boxes[] = [
        'title'        => __( 'Health Fields', 'your-text-domain' ),
        'id'           => 'health-fields',
        'post_types'   => ['health-form'],
        'storage_type' => 'custom_table',
        'table'        => 'wpml_health_form',
        'fields'       => [
            [
                'name'     => __( 'Last Name', 'your-text-domain' ),
                'id'       => $prefix . 'last_name',
                'type'     => 'text',
                'required' => true,
                'columns'  => 6,
            ],
            [
                'name'       => __( 'First Name', 'your-text-domain' ),
                'id'         => $prefix . 'first_name',
                'type'       => 'text',
                'required'   => true,
                'columns'    => 6,
                'first_name' => 'title',
            ],
            [
                'name'    => __( 'Phone Number', 'your-text-domain' ),
                'id'      => $prefix . 'phone_number',
                'type'    => 'tel',
                'columns' => 6,
            ],
            [
                'name'    => __( 'Email', 'your-text-domain' ),
                'id'      => $prefix . 'email',
                'type'    => 'email',
                'columns' => 6,
            ],
            [
                'name'    => __( 'Address', 'your-text-domain' ),
                'id'      => $prefix . 'address',
                'type'    => 'text',
                'columns' => 3,
            ],
            [
                'name'    => __( 'City', 'your-text-domain' ),
                'id'      => $prefix . 'city',
                'type'    => 'text',
                'columns' => 3,
            ],
            [
                'name'    => __( 'Province', 'your-text-domain' ),
                'id'      => $prefix . 'province',
                'type'    => 'text',
                'columns' => 3,
            ],
            [
                'name'    => __( 'Postal Code', 'your-text-domain' ),
                'id'      => $prefix . 'postal_code',
                'type'    => 'text',
                'columns' => 3,
            ],
            [
                'name'    => __( 'Occupation', 'your-text-domain' ),
                'id'      => $prefix . 'occupation',
                'type'    => 'text',
                'columns' => 3,
            ],
            [
                'name'    => __( 'Date of Birth', 'your-text-domain' ),
                'id'      => $prefix . 'date_of_birth',
                'type'    => 'date',
                'columns' => 3,
            ],
            [
                'type' => 'divider',
            ],
            [
                'name'    => __( 'Have you received massage therapy before', 'your-text-domain' ),
                'id'      => $prefix . 'received_massage_therapy',
                'type'    => 'radio',
                'options' => [
                    'Yes' => __( 'Yes', 'your-text-domain' ),
                    'No'  => __( 'No', 'your-text-domain' ),
                ],
                'columns' => 6,
            ],
            [
                'name'    => __( 'Did a healthcare practitioner refer you for massage therapy?', 'your-text-domain' ),
                'id'      => $prefix . 'practitioner_referral',
                'type'    => 'radio',
                'options' => [
                    'Yes' => __( 'Yes', 'your-text-domain' ),
                    'No'  => __( 'No', 'your-text-domain' ),
                ],
                'columns' => 6,
            ],
            [
                'name'    => __( 'Please provide their name', 'your-text-domain' ),
                'id'      => $prefix . 'referall_name',
                'type'    => 'text',
                'columns' => 6,
                'visible' => [
                    'when'     => [['practitioner_referral', '=', 'Yes']],
                    'relation' => 'and',
                ],
            ],
            [
                'name'    => __( 'Please provide their address', 'your-text-domain' ),
                'id'      => $prefix . 'referall_address',
                'type'    => 'text',
                'columns' => 6,
                'visible' => [
                    'when'     => [['practitioner_referral', '=', 'Yes']],
                    'relation' => 'and',
                ],
            ],
            [
                'name'   => __( 'Please indicate conditions you are experiencing or have experienced', 'your-text-domain' ),
                'id'     => $prefix . 'conditions',
                'type'   => 'group',
                'fields' => [
                    [
                        'name'    => __( 'Cardiovascular', 'your-text-domain' ),
                        'id'      => $prefix . 'cardiovascular',
                        'type'    => 'checkbox_list',
                        'options' => [
                            'High Blood Pressure'              => __( 'High Blood Pressure', 'your-text-domain' ),
                            'Low Blood Pressure'               => __( 'Low Blood Pressure', 'your-text-domain' ),
                            'Chronic Congestive Heart Failure' => __( 'Chronic Congestive Heart Failure', 'your-text-domain' ),
                            'Heart Attack'                     => __( 'Heart Attack', 'your-text-domain' ),
                            'Phlebitis/Varicose Veins'         => __( 'Phlebitis/Varicose Veins', 'your-text-domain' ),
                            'Stroke/CVA'                       => __( 'Stroke/CVA', 'your-text-domain' ),
                            'Pacemaker or Similar Device'      => __( 'Pacemaker or Similar Device', 'your-text-domain' ),
                            'Heart Disease'                    => __( 'Heart Disease', 'your-text-domain' ),
                        ],
                        'inline'  => true,
                        'before'  => __( ' ', 'your-text-domain' ),
                    ],
                    [
                        'name'    => __( 'Family history of any of the above?', 'your-text-domain' ),
                        'id'      => $prefix . 'cardiovascular_history',
                        'type'    => 'radio',
                        'options' => [
                            'Yes' => __( 'Yes', 'your-text-domain' ),
                            'No'  => __( 'No', 'your-text-domain' ),
                        ],
                    ],
                    [
                        'name'    => __( 'Respiratory', 'your-text-domain' ),
                        'id'      => $prefix . 'respiratory',
                        'type'    => 'checkbox_list',
                        'options' => [
                            'Chronic Cough'       => __( 'Chronic Cough', 'your-text-domain' ),
                            'Shortness of Breath' => __( 'Shortness of Breath', 'your-text-domain' ),
                            'Bronchitis'          => __( 'Bronchitis', 'your-text-domain' ),
                            'Asthma'              => __( 'Asthma', 'your-text-domain' ),
                            'Emphysema'           => __( 'Emphysema', 'your-text-domain' ),
                        ],
                        'inline'  => true,
                        'before'  => __( ' ', 'your-text-domain' ),
                    ],
                    [
                        'name'    => __( 'Family history of any of the above?', 'your-text-domain' ),
                        'id'      => $prefix . 'respiratory_history',
                        'type'    => 'radio',
                        'options' => [
                            'Yes' => __( 'Yes', 'your-text-domain' ),
                            'No'  => __( 'No', 'your-text-domain' ),
                        ],
                    ],
                    [
                        'name'    => __( 'Infections', 'your-text-domain' ),
                        'id'      => $prefix . 'infections',
                        'type'    => 'checkbox_list',
                        'options' => [
                            'Hepatitis'       => __( 'Hepatitis', 'your-text-domain' ),
                            'Skin Conditions' => __( 'Skin Conditions', 'your-text-domain' ),
                            'TB'              => __( 'TB', 'your-text-domain' ),
                            'HIV'             => __( 'HIV', 'your-text-domain' ),
                            'Herpes'          => __( 'Herpes', 'your-text-domain' ),
                        ],
                        'inline'  => true,
                        'before'  => __( ' ', 'your-text-domain' ),
                    ],
                ],
            ],
        ],
    ];
    
    return $meta_boxes;
    }
    #28828
    Long Nguyen
    Moderator

    Hi Quint,

    Thanks for the additional information.

    Did you replace MetaBoxID with your meta box ID health-fields in the custom code?

    add_action( 'rwmb_health-fields_after_save_post', 'update_post_title' );
    
    #28840
    Qu1nt
    Participant

    Hi Long,

    Yes I did, It's still giving me "no title"

    #28847
    Long Nguyen
    Moderator

    Hi,

    What is the code that you create the custom table to save data? Please share it.

    #28980
    Qu1nt
    Participant

    Hi Long,

    I'm not sure what you mean, I just checked off the 'Save data in a custom table' and named it wpml_health_form in the settings of custom fields which I shared in the post above.

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