I want to convert my old post data containing custom fields that are stored in a text format like : name , url $ name, url $ name, url etc.

I want convert that data and store the new data as an array in a new custom field.
Does anyone know how this can be done?

I want to store that data so that I can use the data with this Gist: Repeatable Custom Fields in a Metabox (Author: Helen Hou-Sandi)

1 Answer 1


Essentially, you need to build an update method for your posts.

The Basics

Your update method needs to get the data of the custom fields, create a new array, and then store that data.

if ( get_post_meta( $post_ID, 'updated', true ) != 1 ) :
$old_data = get_post_custom( $post_ID );
$new_data = array(
     'name' => $old_data[ 'name' ],
     'url'  => $old_data[ 'url' ]

Then, store that data in post meta - it will be serialized.

update_post_meta( 'custom_data', serialize( $new_data ) );

Then, remove the old fields and set a flag to show you've updated:

delete_post_meta( $post_ID, 'name' );
delete_post_meta( $post_ID, 'url' );
update_post_meta( $post_ID, 'updated', 1 );

As for integrating this with the plugin you linked to ... there's a bit more you need to do to get that set up. I leave that as a project for you.

Update: Splitting an existing string

According to your comments, the existing data isn't stored in separate custom fields, but in a single custom field like name, url $ name, url $ name, url. You can split this string into a few arrays and iterate through it to create your new data.

$oldData = "name1, url1 $ name2, url2 $ name3, url3";
$newData = array();
$dataArrays = explode( " $ ", $oldData );

// $dataArrays = ["name1, url1", "name2, url2", "name3, url3"];

foreach( $dataArrays as $dataArray ) {
    $separated = explode( ", ", $dataArray );
    // $separated = ["name1", "url1"];

    array_push( $newData, array(
        'name' => $separated[0],
        'url'  => $separated[1]
    ) );

/* $newData = array(
           'name' => 'name1',
           'url' => 'url1'
           'name' => 'name2',
           'url' => 'url2'
           'name' => 'name3',
           'url' => 'url3'
  • thanks for the help, just wondering how would i split the string (my old data are stored in a one customfield as a string) then pass the data to the new array so that i can be serialised and saved to to custom fields also do i have to go through all my posts and updating the post manually to convert my old data ideally i would want to run the code once and it would update my customfields.
    – user5601
    Commented Apr 3, 2012 at 17:32
  • Please edit your question to include the exact format of the string you're storing already ... But you're either going to need to go through each post and update it, or hook an auto-update function on to WordPress that will update each post when the old data is read out.
    – EAMann
    Commented Apr 3, 2012 at 18:34
  • am storing the sting like this on the custom-field name, url $ name, url $ name, url $ name, url $ name, url
    – user5601
    Commented Apr 3, 2012 at 18:40
  • thanks a lot you saved me a lot of time just need to figure out how to run the code for all my post.=)
    – user5601
    Commented Apr 3, 2012 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.