Support

Account

Home Forums Add-ons Flexible Content Field Convert Repeater to Flexible field — after content's been added

Solved

Convert Repeater to Flexible field — after content's been added

  • Hello,

    I’ve built a template with a repeater field and all the content’s been uploaded — many pages and lots of data. My client is now requesting an alternate layout for this page template, which means I need to switch to a Flexible field.

    I did a simple experiment and the repeater field data is no longer visible after switching field types — even though I used the same field/sub-field names. I know that the data is still in the database, but I’m guessing this is a path issue since the old repeater field data is accessible on a different path in the DB than the new Flexible field data would be.

    Can anyone tell me how to switch field types without having to re-enter all the data?

    In summary: I have a REPEATER field and I want to convert it to a FLEXIBLE field and preserve all the data we entered already, but all the content disappears from view when I do the switch.

    Thanks

  • Hi @SQD

    Thanks for the question. You are quite right, the data is still intact in your DB and it is possible to change the field type from repeater to flexible content.

    Both field types save the sub field data in the same way, the only difference is the way they save their own reference data. By this, I mean how many rows are saved.

    The repeater field will simply save a number like so: 3
    This means that there are 3 rows of data available, and the repeater field will load them in.

    The flexible content field does not save just a number, instead it save’s an array of ‘layout’ names like so: array('content', 'images', 'download')
    This means that there are 3 rows of data available, and the flexible content can load the appropriate ‘layout’ and the data.

    So, all you need to do is edit your DB (wp_postmeta table) where the meta_key is your repeater field name. The value will be a number, and you will need to change it to a SERIALIZED array containing the correct number of layout names.

    Hope that helps.

    Thanks
    E

  • Thanks Elliot. This makes sense. I solved my immediate problem with a logic work-around, but this would be a great thing to know how to do for future projects.

    Best,
    Seth

  • Sorry to resurrect this one. Seems I have come across this issue myself. So I have cleaned out all revisions and queried the db for the repeater field I am using:

    Here are the results

    79 30 page_blocks
    1997 3876 page_blocks 2
    2222 3882 page_blocks 3
    3311 3927 page_blocks 6
    5228 4002 page_blocks 1

    Each is a page in the site. So the process is:

    Change the field type in the ACF editor to Flexible Content

    Then specifically how do I figure out what serialized array to enter into each page?

  • If the flex field has the same name as the repeater and all the sub fields of the first layout are exactly the same as the old sub fields then you need to do something like this

    
    // query all of the posts of the post type
    $args = array(/* your query args here */);
    
    $query = new WP_Query($args);
    while ($query->have_posts()) {
      $query->the_post();
      // get the number of rows in the array
      $rows = get_post_meta($post->ID, 'repeater_field_name');
      // create an array containing the first layout $rows times
      $value = array_fill(0, $rows, 'first_layout_name');
      // update with the new value
      update_post_meta($post->ID, 'repeater_field_name', $value);
    }
    
    

    I think this would need to be done after changing the field type but before attempting to update any posts that use the field.

    This can probably also be done with straight db queries, but I’m not sure how to do that.

    As always, when making large changes to the database always back it up first.

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

The topic ‘Convert Repeater to Flexible field — after content's been added’ is closed to new replies.