Support Forum
Support › Meta Box Group › update_post_meta for cloneable group fields
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
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 );
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 );
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 );
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
);
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
);