Set Post Title from Field Group Submission data
- This topic has 5 replies, 2 voices, and was last updated 3 years, 3 months ago by
Macky McCormack.
-
AuthorPosts
-
January 13, 2022 at 8:55 PM #33204
Macky McCormack
ParticipantHi,
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_nameThe 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
January 13, 2022 at 10:00 PM #33206Long Nguyen
ModeratorHi,
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
January 13, 2022 at 11:41 PM #33212Macky McCormack
ParticipantHere 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; }
January 14, 2022 at 12:56 PM #33219Long Nguyen
ModeratorHi,
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 ); }
January 19, 2022 at 5:28 PM #33328Macky McCormack
ParticipantHi 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?
January 19, 2022 at 5:31 PM #33329 -
AuthorPosts
- You must be logged in to reply to this topic.