Support Forum
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:
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?
Hi,
To get the subfield value, please follow this documentation https://docs.metabox.io/extensions/meta-box-group/#examples
For example:
$group = rwmb_meta( 'event_details', '', $post_id );
$t1 = $group['event_name'];
$t2 = $group['event_start_date'];
thanks Long. that did the trick!
Should this work with frontend submissions? Not working in front end for me.
Hi,
Yes, the code can work in both frontend and backend. You can share the code that creates the fields and update the title on your site, I will help you to check the issue.