Set Post Title from Field Group Submission data

Support General Set Post Title from Field Group Submission data

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #33204
    Macky McCormackMacky McCormack
    Participant

    Hi,

    I want to set the title of a custom post using data from two fields (first name and last name) when the post is created. I have seen other forum posts on how to combine two fields to another field and have tried a code snippet but its not working for me. I am not a PHP coder so am struggling to understand how to get this to work.

    Can you help me set this up?

    Field IDs:

    First name - customer_first_name
    Last name - customer_last_name

    The field group is set up and working - when I input the data a post is created. All I need to do is set the post title as 'customer_first_name customer_last_name'

    Many thanks

    #33206
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can refer to this topic to know how to update post title from the custom field value https://support.metabox.io/topic/using-custom-meta-fields-to-create-cpt-title/

    If it does not work, please share the code that creates the custom fields on your case, I will help you to correct the code. With the builder, please follow this documentation to get PHP code https://docs.metabox.io/extensions/meta-box-builder/#getting-php-code

    #33212
    Macky McCormackMacky McCormack
    Participant

    Here is the PHP from the builder

    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
    $meta_boxes[] = [
        'title'        => __( 'Retreat Guest Info', 'your-text-domain' ),
        'id'           => 'retreat-guest-info',
        'post_types'   => ['retreat-info'],
        'storage_type' => 'custom_table',
        'table'        => 'wp_retreat_guest_info',
        'fields'       => [
            [
                'name'     => __( 'First Name', 'your-text-domain' ),
                'id'       => $prefix . 'customer_first_name',
                'type'     => 'text',
                'required' => true,
            ],
            [
                'name'     => __( 'Last Name', 'your-text-domain' ),
                'id'       => $prefix . 'customer_last_name',
                'type'     => 'text',
                'required' => true,
            ],
        ],
    ];
    
    return $meta_boxes;
    
    }
    
    #33219
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can try to use this code

    add_action( 'rwmb_retreat-guest-info_after_save_post', 'update_event_title' );
    function update_event_title( $post_id ) {
        $first_name = rwmb_meta( 'customer_first_name', ['storage_type' => 'custom_table', 'table' => 'wp_retreat_guest_info'] , $post_id );
        $last_name = rwmb_meta( 'customer_last_name', ['storage_type' => 'custom_table', 'table' => 'wp_retreat_guest_info'], $post_id );
        
        $my_post = array(
            'ID' => $post_id,
            'post_title' => $first_name . '-' . $last_name
        );
        wp_update_post( $my_post );
    }
    #33328
    Macky McCormackMacky McCormack
    Participant

    Hi Long,

    Many thanks for your reply. I have added your code but the post title still remains (no title) when I submit. Can you suggest anything to fix this?

    #33329
    Macky McCormackMacky McCormack
    Participant
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.