Custom Fields
Re-Envisioned
Support Meta Box Group Existing Values Before Group was Created

  • Creator
    Topic
  • #1059
    Scriptonite
    Participant

    I have added the group extension to a client site to extend an existing cloneable text field. How can I convert the current data via script or filter to be compatible with the new group?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #1071
    Anh Tran
    Keymaster

    Hi, in order to convert data from 1 format to another format, you need to understand how data is saved in the database. Here is the documentation for this. For group, data is saved as an array, each element of which is the value of sub-fields.

    In this case, if you are using simple text field, your data is just multi rows in the database, so getting values will not be a big problem. But I'm not clear how you want to put them in a group? Will the text field is a sub-field of the new group? If so, you can use the following code:

    <?php
    $text_id = 'TEXT_FIELD_ID';
    $group_id = 'GROUP_FIELD_ID';
    
    $posts = get_posts( array(
    	'posts_per_page' => -1,
    ) );
    foreach ( $posts as $post )
    {
    	$text = rwmb_meta( $text_id, 'type=text', $post->ID );
    
    	$meta_key = "{$group_id}[{$text_id}]";
    	update_post_meta( $post->ID, $meta_key, $text );
    
    	// delete_post_meta( $post->ID, $text_id ); // Remove old data, use with care
    }
    #1072
    Scriptonite
    Participant

    Thanks, I took care of it the next day after I did not get any reply by just querying all the usermeta keys, unserializing the exisitng arrays, and constructing the new arrays. I was looking for a quick answer to save time searching through the documentation and writing a code snippet since it seems like a feature that would be in place.

    This was an extremely disappointing response time for a paid plugin.

    #1073
    Anh Tran
    Keymaster

    I'm really sorry that you have spent much of time searching for the solution. I was on a holiday last week and couldn't answer you sooner. Hope you be generous and still love the plugin.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Existing Values Before Group was Created’ is closed to new replies.