Support

Account

Home Forums General Issues Help converting an existing custom field into ACF?

Solving

Help converting an existing custom field into ACF?

  • Hey there — I’ve got a problem I’m hoping someone can help me with. I’ve got an old WP site for which I set up a series of custom fields about five years ago. I’m hoping to convert them over into being manageable via ACF. Following John Huebner’s advice here, I just set the names of my ACF fields to be the same as my existing custom fields, then commented out my original functions.php code that created the old fields. And in most cases — all the cases in which the custom field was either text or a textarea — it worked great!

    But it doesn’t work at all when I tried it with the custom fields that are true/false. I create a field group in ACF, set it to true/false and with the same field name, and comment out my original functions.php code —— but it doesn’t pick up the existing true/false data from the custom fields. I’m wondering if it has anything to do with there being a “Default Value” for the ACF true/false field? Perhaps that overrides the existing checkbox info?

    Anyway, I’ve been banging my head against the wall on this and would love any advice or wisdom anyone might have about it. This is thousands of posts we’re talking about, so I really can’t just go through each one and reenter the checkbox data from scratch on each. Thanks!

  • For simple text based fields, just setting up the fields in ACF usually works. This includes text, textarea, number, url, email, password, radio, and single select type fields (although the number fields will return numbers as text, so that could be a problem).

    With other types of fields you need to also create the field key values in the database and the values stored in them need to match the values that ACF stores. For example, ACF stores a “1” or a “0” for true/false fields. If you where storing true and false using WP then the values in the database are being stored as serialized values representing true and false.

    If the values stored are exactly the same then you can get ACF to recognize and output the other types of fields by adding the field key to the database for each field. This can be somewhat problematic since you need to have an entry added for every post that has a value stored for it in the custom field.

    Here’s a simple script that can do this, although it could time out if you have a lot of posts to deal with (you could set this up as a cron). But again, this will only work if the type of data stored is the same as well. I would also make sure I backed up my database before trying it.

    
    // set up an array with all of the field names 
    // and field keys that you've created in ACF
    // this will not work on repeater or flex content fields and sub fields
    $fields = array(
      'field_name_1' => 'field_key_1',
      'field_name_2' => 'field_key_2',
      // etc...
    );
    // get every post
    $args = array(
      'post_type' => 'any',
      'post_status' => 'any',
      'posts_per_page' => -1
    )
    $query = new WP_Query($args);
    if (count($query->posts)) {
      foreach ($query->posts as $post) {
        $post_id = $post->ID;
        // get all post meta for the post can speed 
        // things up we can actually ignore $meta but 
        // calling this way forces WP to get all meta 
        // values for a post in one query
        $meta = get_post_meta($post_id);
        foreach ($fields as $field_name => $field_key) {
          $value = get_post_meta($post_id, $field_name, true);
          if ($value != '') {
            update_post_meta($post_id, '_'.$field_name, $field_key);
          }
        }
      }
    }
    

    there are probably other creative ways to do this. Would definitely be faster if you queried the database directly using $wpdb, but I’m not sure of the details on how to do that.

  • This is tremendously helpful — thanks so much! Will try this out tomorrow. Had noticed the true/false vs. 0/1 issue in the database and wondered if that was part of it.

  • You may also want to handle converting data at the same time. For example changing true/false to 1/0.

    multi-select field and checkbox fields store serialized arrays in ACF.

    If you have any other fields that are not text based I can tell you the type of data that is stored by ACF.

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

The topic ‘Help converting an existing custom field into ACF?’ is closed to new replies.