Support

Account

Home Forums Add-ons Flexible Content Field Can I get wp_postmeta.meta_id ? Reply To: Can I get wp_postmeta.meta_id ?

  • I do this in Javascript.

    The first thing that I do is to create a text field and then add https://www.advancedcustomfields.com/resources/acf-prepare_field/ to make the field readonly.

    
    (function($){
      if (typeof acf != 'undefined') {
        acf.add_action('ready append', function($el) {
          
          // generate ID values
          var ids = [];
          // use the field key of your text sub field
          var fields = $('[data-key="field_5dd6ea9c6325c"] .acf-input input').not('.clones [data-key="field_5dd6ea9c6325c"] .acf-input input');
          if (fields.length) {
            var prefix = 'pnl-';
            // I also have my scripts localized with this object
            // it contains the post ID
            // so that I am sure the ID is unique on every post
            if (typeof(theme_admin_values['post']['id']) != 'undefined') {
              prefix += theme_admin_values['post']['id']+'-';
            }
            for (i=0; i<fields.length; i++) {
              var field = $(fields[i]);
              var value = field.val();
              if (value == '' || panel_ids.indexOf(value) != -1) {
                value = prefix+acf.uniqid();
                field.val(value);
                field.trigger('change');
              }
              panel_ids.push(value);
            }
          }
          // end generate ID values
          
        }); // end acf.add_action
      } // end if acf definded
    })(jQuery);
    

    I do this for several fields, each has it’s own prefix. No, they are not pretty ID values, but I know they are not going to change. This script is a little slow because every field is checked every time a new row is added. I haven’t worked out how to only work on new rows without looking at the existing ones.