Migrating from regular fields to a grouped field
- This topic has 3 replies, 2 voices, and was last updated 3 years, 1 month ago by
Jonathan Minter.
-
AuthorPosts
-
February 17, 2022 at 10:59 PM #33965
Jonathan Minter
ParticipantIs there a resource somewhere that would help me understand how to take a set of regular MetaBox fields I've been using on a custom post type, and then migrate them to a new grouped field? I have to confess I've never dealt with raw serialized data in a database before...
My thoughts were that I could dump the data from my existing wp_postmeta table and then build some insert statements. I'm doing a fair amount of restructuring, so I now have groups to represent what used to be regular MB fields as well as some taxonomies attached to the post type that I now want inside the groups.
But if I knew how to just take one regular field and migrate it to a grouped field, I could extrapolate and handle the rest of it.
Trying to avoid touching hundreds of posts manually...
February 18, 2022 at 12:36 PM #33978Long Nguyen
ModeratorHi,
I think it could be done with some steps:
- Make sure that register the correct group field and subfield.
- Create a function that hooks to the action after saving post likesave_post
- This function will take the field value and assign it to a group ID array
- serialize data and update the fieldgroup
valueHere is a sample code
add_action( 'save_post', function( $post_id ) { $group = array(); $field_id1 = rwmb_meta( 'field_id1', '', $post_id ); $field_id2 = rwmb_meta( 'field_id2', '', $post_id ); $group['field_id1'] = $field_id1; $group['field_id2'] = $field_id2; $group_serialize = maybe_serialize( $group ); rwmb_set_meta( $post_id, 'group_id', $group_serialize ); } );
Refer to the documentation https://docs.metabox.io/extensions/meta-box-group/#getting-sub-field-values
https://docs.metabox.io/rwmb-set-meta/February 19, 2022 at 12:31 AM #33993Jonathan Minter
ParticipantOkay, I see what you are saying here. I might actually create a custom function to do this and then have it loop through all my existing posts and do a one-time conversion from the old fields to the new grouped field. But I think I can step forward based on the approach you've outlined here. Thanks!
March 5, 2022 at 3:11 AM #34319Jonathan Minter
ParticipantI'm having some trouble with this. I've taken the snippet you gave me, plus some of the documentation on rwmb_set_values, put this inside init with priority 99 as recommended. This will be a one-time process and then I will remove it. But the rmwb_set_values function is doing nothing.
add_action( 'init', function() { $sermons_a = array( 'post_type' => 'message', 'order' => 'DESC', 'nopaging' => true, ); $sermons_query = new WP_Query( $sermons_a ); if ( $sermons_query->have_posts() ) { while ( $sermons_query->have_posts() ) { $sermons_query->the_post(); // Get existing data $temp_audio = rwmb_meta( 'audio', '', get_the_ID() ); $temp_video = rwmb_meta( 'video-id', '', get_the_ID() ); $speakers = get_the_terms( get_the_ID(), 'speaker' ); $groups = array(); // since this is a cloneable field, create an array of group values $group = array(); // individual group value $group['message_media_campus'] = 12; // literal value here if ($speakers) { $group['message_media_speaker'] = $speakers[0]->term_id; // only using the first speaker } $group['message_media_audio'] = $temp_audio; $group['message_media_video-id'] = $temp_video; $groups[] = $group; $group_serialize = maybe_serialize( $groups ); rwmb_set_meta( get_the_ID(), 'message_media_group', $group_serialize ); } // end while } // endif }, 99 );
-
AuthorPosts
- You must be logged in to reply to this topic.