Using custom meta fields to create CPT title

Support General Using custom meta fields to create CPT titleResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #29073
    @mindspark@mindspark
    Participant

    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?

    #29081
    Long NguyenLong Nguyen
    Moderator

    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'];
    
    #29130
    @mindspark@mindspark
    Participant

    thanks Long. that did the trick!

    #34234
    FanomenalFanomenal
    Participant

    Should this work with frontend submissions? Not working in front end for me.

    #34249
    Long NguyenLong Nguyen
    Moderator

    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.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.