update_post_meta for cloneable group fields

Support MB Group update_post_meta for cloneable group fieldsResolved

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #16552
    Manoj KumarManoj Kumar
    Participant

    Hi,

    I need to update group cloneable meta fields programmatically. So, that I can loop through an array and update the post meta fields.

    Thanks in advance

    #16591
    Anh TranAnh Tran
    Keymaster

    Hi Manoj,

    To update a cloneable group field, please use this snippet:

    $values = [];
    $values[] = [ 'sub_field_1' => 'value1', 'sub_field_2' => 'value2' ]; // Value of clone 1
    $values[] = [ 'sub_field_1' => 'value1', 'sub_field_2' => 'value2' ]; // Value of clone 2
    
    update_post_meta( $post_id, 'group_id', $values );
    #36844
    vacdkvacdk
    Participant

    How do i Update just 1 field in a clonealbe group ?

    Right now i use this code ud output data after submission:

    $group_values = rwmb_meta( 'group_tapgyiu2mz', '', $post_id );
                if ( ! empty( $group_values ) ) {
                    foreach ( $group_values as $group_value ) {
                     $rank = isset( $group_value[ 'ticket_rank' ] ) ? $group_value[ 'ticket_rank' ] : '';
                     echo $rank; // Display sub-field value  
                    }
                }
    
                die();
    

    That works fine

    but then I want also to update a group field at the same time in the group.

    add_action( 'init', function() {
        $post_id = 123;
        $field_id = 'group_tapgyiu2mz';
        $value = [
            [
                'hidden_field' => '1234',
            ],
        ];
        rwmb_set_meta( $post_id, $field_id, $value );
    }, 99 );
    #36855
    Long NguyenLong Nguyen
    Moderator

    Hi vacdk,

    You need to get the group value first, access the element, and assign another value for this.

        $post_id = 123;
        $value = rwmb_meta( 'group_tapgyiu2mz', '', $post_id );
        $value[0]['hidden_field'] = '1234';
        rwmb_set_meta( $post_id, $field_id, $value );
    #36857
    vacdkvacdk
    Participant

    Is this the same if it's a cloneable group ? as I can't get it to work. :/
    where is it that I target the specified group field row in the foreach ?

    
    add_action("rwmb_frontend_after_process", function ($config, $post_id) {
            if ("ticket-addon" === $config["id"]) {
    
            $group_values = rwmb_meta( 'group_tapgyiu2mz', '', $post_id );
                if ( ! empty( $group_values ) ) {
                    foreach ( $group_values as $group_value ) {
                     $rank = isset( $group_value[ 'ticket_rank' ] ) ? $group_value[ 'ticket_rank' ] : '';
                     echo $rank; // Display sub-field value  
    
                     //$post_id = 123; <-- getting post ID from the add_action
                     $value = rwmb_meta( 'group_tapgyiu2mz', '', $post_id );
                     $value[0]['ticket_row_id'] = '1234';
                     rwmb_set_meta( $post_id, $field_id, $value );
                    }
                }
    
                die();
            }
        },
        10,
        2
    );
    
    #36899
    Long NguyenLong Nguyen
    Moderator

    Hi,

    If you get the group value above with this code

    $group_values = rwmb_meta( 'group_tapgyiu2mz', '', $post_id );

    please remove itself from the loop

    add_action("rwmb_frontend_after_process", function ($config, $post_id) {
            if ("ticket-addon" === $config["id"]) {
    
            $group_values = rwmb_meta( 'group_tapgyiu2mz', '', $post_id );
                if ( ! empty( $group_values ) ) {
                    foreach ( $group_values as $key => $group_value ) {
                     $rank = isset( $group_value[ 'ticket_rank' ] ) ? $group_value[ 'ticket_rank' ] : '';
                     echo $rank; // Display sub-field value  
    
                     //$post_id = 123; <-- getting post ID from the add_action
                     //$value = rwmb_meta( 'group_tapgyiu2mz', '', $post_id ); <--remove the duplicate code to get group value here
                     if( $key == 0 ) $group_values[$key]['ticket_row_id'] = '1234'; //update field ticket_row_id for the first clone group only, for example
                     
                    }
                }
                rwmb_set_meta( $post_id, 'group_tapgyiu2mz', $value ); //move this code outside the loop because we need to update the group field with ID
    
                die();
            }
        },
        10,
        2
    );
    #39043
    Macky McCormackMacky McCormack
    Participant

    Hi,

    I'm trying to get this to work but for some reason its not. Here's my fields

    
    $meta_boxes[] = [
            'title'      => __( 'Order Details', 'your-text-domain' ),
            'id'         => 186,
            'post_types' => ['fr_order'],
            'style'      => 'seamless',
            'fields'     => [
                [
                    'name'   => __( 'Guest Names', 'your-text-domain' ),
                    'id'     => $prefix . 'fr_guest_names',
                    'type'   => 'group',
                    'fields' => [
                        [
                            'name'  => __( 'Guest Name', 'your-text-domain' ),
                            'id'    => $prefix . 'fr_guest_name',
                            'type'  => 'text',
                            'clone' => true,
                        ],
                    ],
                ],
            ];
    

    and here's my code for updating:

    
    $post_meta = Array
    (
        [0] => Array
            (
                [fr_guest_name] => Amet tempor nisi
            )
    
        [1] => Array
            (
                [fr_guest_name] => Condimentum a
            )
    
        [2] => Array
            (
                [fr_guest_name] => Mollis sed posue
            )
    
        [3] => Array
            (
                [fr_guest_name] => Arcu a eget euis
            )
    )
    
    update_post_meta($order_id, 'fr_guest_names', $post_meta);
    

    The post has already been created and the $order_id is the wp post id.

    I get a meta ID returned from the update_post_meta function but the names don't show up in the admin area:

    https://www.dropbox.com/s/luoigwe59tqzq3x/metaboxclonablegroup.JPG?dl=0

    Can you help?

    #39044
    Macky McCormackMacky McCormack
    Participant

    EDIT - I've checked the DB and the serialised array is present in the wp_postmeta table

    #39195
    Macky McCormackMacky McCormack
    Participant

    Hi, I submitted this a week ago, could you have a look and help me out?

    Thanks

    #39196
    Macky McCormackMacky McCormack
    Participant

    I'll move to a new post as this is marked resolved

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