Using custom meta fields to create CPT title
- This topic has 4 replies, 3 voices, and was last updated 3 years, 7 months ago by
Long Nguyen.
-
AuthorPosts
-
June 23, 2021 at 2:50 AM #29073
@mindspark
ParticipantI'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?
June 23, 2021 at 4:53 PM #29081Long Nguyen
ModeratorHi,
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'];June 26, 2021 at 6:00 AM #29130@mindspark
Participantthanks Long. that did the trick!
March 2, 2022 at 7:58 PM #34234Fanomenal
ParticipantShould this work with frontend submissions? Not working in front end for me.
March 3, 2022 at 11:13 AM #34249Long Nguyen
ModeratorHi,
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.
-
AuthorPosts
- You must be logged in to reply to this topic.