Support

Account

Home Forums Feature Requests Change field type or group and retrieve values

Solving

Change field type or group and retrieve values

    • feyfey

    • December 14, 2019 at 10:02 pm

    Hi all,

    Because off experiencing our CPT with ACF (PRO) field make us change our minds sometimes I need to change field type, field name, field choice, get field outside a group, …

    So, Does a tool exist to easily change database data to retrieve values in field ? I know I can made this change directly on BDD but it’s not fast and simple.

    The hardest for me is if I put a field outside its original group field, or if I turn a file field type to gallery one.

    If someone know a tool or code …

    Thanks all =)

  • No, a tool does not exist to do this. If you move fields around (in or out of repeaters or group fields) or you rename them then the values already save will not be accessible.

    Altering the database to reflect any changes would be completely dependent on the exact changes made and could likely cause other things to break.

    There are paths for making these changes but there is nothing simple, and again, it would be dependent on the changes made.

    Let’s say for example that I want to do something as simple as change a field name, instead of changing the field name I would leave the old field alone and add a new field with the new name. Then I would add several filters.

    Please not that this us just an example and it may not work for your specific field, it is just a starting place.

    
    
    // use the field key of the old field
    add_filter('acf/prepare_field/key=field_XXXXX', 'hide_old_field');
    function hide_old_field($field) {
      // this prevents displaying the field so the value can no longer be edited
      // but any existing value will be preserved
      return false;
    }
    
    // load the old value when getting the new field
    add_filter(acf/load_value/name=your-new-field-name', 'load_old_value_in_new_field', 1, 3);
    function load_old_value_in_new_field($value, $post_id, $field) {
      if ($value === NULL) {
        // a value was never saved for this field
        // get the value from the old field
        $value = get_field('old_field_name', $post_id);
      }
      return $value;
    }
    
    • feyfey

    • January 4, 2020 at 11:48 pm

    Hi Jhon,

    Thanks for your answer. I want to change field for several reasons but one of them is I need to create statistic and in this specific case I can’t use your solution because it’s will be very too long to edit and save all our old post.

    For exemple I need to :

    • Change text to select field : To made statistic I need to change text field to a select one because user never wrote value the same way. So I need to edit all the value by meta_key and depending on keywords.

    I probabely can use your filter but did you know a solution to apply filter to all post (by PT for exemple) and save ? For exemple load mass change from a option page I create ?

    Appart from this your function is very usefull and I probably can edit it to fit for many case.

  • Changing a text field to a select field is actually not too difficult.

    If you make the select field that only allows a single choice then the value stored in the DB is a single text value, just like the text field. Simply changing the field type will allow it to continue working, you just need to make sure that every value that has ever been entered is a choice in the field.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.