update_post_meta for cloneable group fields
- This topic has 9 replies, 5 voices, and was last updated 2 years, 5 months ago by
Macky McCormack.
-
AuthorPosts
-
October 16, 2019 at 9:15 PM #16552
Manoj Kumar
ParticipantHi,
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
October 18, 2019 at 3:43 PM #16591Anh Tran
KeymasterHi 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 );
July 7, 2022 at 4:36 AM #36844vacdk
ParticipantHow 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 );
July 7, 2022 at 12:30 PM #36855Long Nguyen
ModeratorHi 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 );
July 7, 2022 at 1:29 PM #36857vacdk
ParticipantIs 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 );
July 9, 2022 at 11:00 PM #36899Long Nguyen
ModeratorHi,
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 );
November 10, 2022 at 5:58 PM #39043Macky McCormack
ParticipantHi,
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?
November 10, 2022 at 6:08 PM #39044Macky McCormack
ParticipantEDIT - I've checked the DB and the serialised array is present in the wp_postmeta table
November 17, 2022 at 11:58 PM #39195Macky McCormack
ParticipantHi, I submitted this a week ago, could you have a look and help me out?
Thanks
November 17, 2022 at 11:59 PM #39196Macky McCormack
ParticipantI'll move to a new post as this is marked resolved
-
AuthorPosts
- You must be logged in to reply to this topic.