Support Forum
Hi,
I was able to drag some existing fields into a group field. but when I do, I lose the value. When I drag them back them I can see the value is still there. I want to therefore set the new group field value with the old value. I tried to use:
add_action( 'init', function() {
$post_id = $postID;
$field_id = 'the_field_within_the_groups_ID';
$value = [
[
$field_id => 'Andy',
],
];
rwmb_set_meta( $post_id, $field_id, $value );
}, 99 );
But in this example from your documentation, I do not understand where it needs the group value. Maybe I am getting confused by all the field IDs. Can I clarify what the field_id is? Is this the field ID of the field inside the group, is it the group id or is it the field_id of the entire custom group?
For my case I have:
Field Group ID = Research (so this is for all the below custom fields)
$prefix = academic
$IDofTheOriginalFieldIWantToMoveTheValueFrom = $prefix.$year
$GroupFieldID = $prefix.$Reference
$FieldIDInsideTheGroup = $prefix.$year //It is the same as the original as I dragged it into the group, but the values did not transfer,
Please let me know how I can fix this so that the value of $FieldIDInsideTheGroup becomes $IDofTheOriginalFieldIWantToMoveTheValueFrom
And just so you can see where I got up to (might be easier to explain where its going wrong):
add_action('init', function() {
$args = array(
'post_type' => 'research',
'order' => 'DESC',
'nopaging' => true,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$valueToCopy = rwmb_meta('academicyear', '', $post->ID); // $IDofTheOriginalFieldIWantToMoveTheValueFrom
$year_groups = rwmb_meta('academicreference_group', '', $post->ID) ?? ''; // $GroupFieldID
foreach ($year_groups as $year_group) {
$year = $year_group['academicyear'] ?? ''; // $FieldIDInsideTheGroup
}
rwmb_set_meta($post->ID, $year, $valueToCopy);
;
}}}, 99);
Hello,
This need to have some custom code to move data from a top field to a group field. You can refer to this topic to have a basic knowledge about that https://support.metabox.io/topic/migrating-from-regular-fields-to-a-grouped-field/#post-34319
If you are not able to complete the task, you can contact us here https://metabox.io/contact/
our development team will help you with an extra fee.
Hi Peter,
Thank you for that! I was able to migrate all text, links etc.
However, I am really strugling with taxonomies.
I noticed that when I pulled them into the group, the post was disassociated with the term. I fixed it by reuploading the data into another field outside the group. But I want to pull that data into the group.
I think I can get the terms, but then it does not work the same way as the text fields. I cannot insert and save.
Help would be hugely appreciated. Just the functions I can use to do this successfully.
And in case anyone comes across this thread - this is how I did the text fields:
add_action('init', 'migratethefields', 99);
function migratethefields() {
$fields = array('year','peer-review', 'doi', 'co_authorYN', 'citation');
$prefix = 'academic';
$fieldgroup = 'reference_group';
$group_field_ID = $prefix . $fieldgroup;
foreach ($fields as $field) {
$new = 'justtostoptheinit';
$field_ID = $prefix . $field;
if (get_option($new . $field_ID)) {
continue;
}
$args = array(
'post_type' => 'research',
'posts_per_page' => -1,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_ID = get_the_ID();
$valuestotransfer = null;
$valuestotransfer = rwmb_meta($field_ID, '', $post_ID);
if (empty($valuestotransfer)) {
$valuestotransfer = rwmb_get_value ($field_ID, '', $post_ID);
}
if (is_array($valuestotransfer) && isset($valuestotransfer[0]) && is_object($valuestotransfer[0])) {
$valuestotransfer = array_map(function($term) {
return $term->term_id;
}, $valuestotransfer);
}
$group_field = rwmb_meta($group_field_ID, '', $post_ID);
if (!empty($valuestotransfer) && is_array($group_field) && isset($group_field[0])) {
$group_field[0][$field_ID] = $valuestotransfer;
}
$updated = update_post_meta($post_ID, $group_field_ID, $group_field);
}
}