I've been using the following code successfully for some time. It allows me to leave a post title field blank and instead, combine 2 meta fields to create the post title. see below:
add_action( 'rwmb_event-fields_after_save_post', 'update_event_title' );
function update_event_title( $post_id ) {
$t1 = rwmb_meta( 'event_name', '', $post_id );
$t2 = rwmb_meta( 'event_start', '', $post_id );
$my_post_title = get_the_title( $post_id );
$my_post = array(
'ID' => $post_id,
'post_title' => $t1 . '-' . $t2,
'post_name' => $t1 . '-' . $t2
);
wp_update_post( $my_post );
}
but, now I'm doing something more complex and the above code format isn't working for me.
I have a CPT 'Event' and a custom field group 'Create Event Fields'. in the field group, I am using a GROUP with ID 'event_details' and within that group are 4 meta fields:
- Event Name (event_name)
- Event Start Date (event_start_date)
- Event Start Time (event_start_time)
- Event End Time (event_end_time)
I'm trying to use the following to combine (event_name) and (event_date) to create the ppost title, but it's not working. I think it's because these fields are contained within a group, but I've tried many variations to call the correct fields and I can't figure it out. Here is my last try:
add_action( 'rwmb_create-event-fields_after_save_post', 'update_event_title' );
function update_event_title( $post_id ) {
$t1 = rwmb_meta( 'event_details_event_name', '', $post_id );
$t2 = rwmb_meta( 'event_details_event_start_date', '', $post_id );
$my_post_title = get_the_title( $post_id );
$my_post = array(
'ID' => $post_id,
'post_title' => $t1 . '-' . $t2,
'post_name' => $t1 . '-' . $t2
);
wp_update_post( $my_post );
}
Any suggestions?