Support

Account

Home Forums General Issues Convert plain text to Importable Post Object

Solved

Convert plain text to Importable Post Object

  • I’m in the process of building a CRM for a music shop using mostly Custom fields and custom post types. The biggest challenge is that the current database has all the instruments that each customer has in plain text – for example, ‘PianoA KeyboardF OrganB’.

    I’d like to try to convert all these plain text instruments into the existing product post objects.

    I’ve made a field for this to use on customer user profiles and tried exporting customers to see how it would come out. I found it comes out as ‘a:1:{i:0;s:5:”10371″;}’.

    It’s not going to be possible for me to easily use this format across hundreds of instruments and thousands of people on this database. Is there any way to use simple IDs separated by commas for example? That way I could simply use string replaces to make the data work.

    Any ideas appreciated…

  • ACF stores a relationship field as an array of post IDs. What you are seeing is a serialized array in the DB.

    What you need to do is convert the list of names to an array then find the post ID for each of those posts and store these into an array and then use this array to update the ACF field.

  • Ahh, that makes sense – not sure how I missed that looking at it now. I’ve managed to convert my data to JSON in the spreadsheet with simple regex replace – do you know if ACF will take that or do I need to further convert the JSON to serialized?

  • You just need to convert the json to an array. ACF will use the array value.

    
    $posts = array(1,2,3,4,5);
    update_field($selector, $posts, $post_id);
    
  • I think I’m still missing a piece of the puzzle. Perhaps I’m missing something obvious.

    In the spreadsheet is 14,000 records with JSON such as [“5667″,”93872”] for my custom field. I was planning on using a plugin to import this spreadsheet and create the new users. Do I need to format the sheet further before importing or is that code you provided to fix it after it’s imported?

    Thanks for your help 🙂

  • You can’t import the json directly to ACF fields. The value must be an array. If you try to import an already serialize array you may end up with double serialization and that would be as bad as the json strings.

  • Ahh, I see – so it will take an array and serialize it when it gets imported? That makes more sense…

    So it should be like:

    Array
    (
    [0] => 9801
    [1] => 10371
    )

    or this?

    array(9801,10371)

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.