Custom Fields
Re-Envisioned
Support General Set Post Title from Field Group Submission data

  • Creator
    Topic
  • #33204
    Macky 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #33206
    Long Nguyen
    Moderator

    Hi,

    You can refer to this topic to know how to update post title from the custom field value https://metabox.io/support/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 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 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 );
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.