Support

Account

Home Forums Add-ons Repeater Field Adding subfields to Repeater Field

Solved

Adding subfields to Repeater Field

  • Hey all,

    I have an importer script which grabs data off the old database and inserts them into a post, I use update_field to add each item and it works great.

    The issue occurs when I try to add additional repeater fields sub fields, there are a variable number of images per post and I need to add them into the repeater as I retrieve them.

    If I manually go in and create the fields through the GUI I can use update_sub_field to update it. But if the field doesn’t exist in the repeater field already then nothing happens. Why can’t I create an additional field using this function, is there a work around?

    How do I add another row to the repeater field programatically with PHP? If I can add a dummy row then I can use update_sub_field to update it to the correct value.

  • Take a look at the section on this page about field_key vs field_name. Basically, if the field does not exist yet you need to use the field_key. http://www.advancedcustomfields.com/resources/update_field/

  • Thanks for the reply, I read the documentation. The problem is that if I insert a post the field isn’t created so I cant get the field key. The issue isn’t updating the repeater fields, or having a field key. The issue is adding to the field – I hope that makes sense.

    I am trying to work out a workaround, I am currently inserting dummy rows to at least make the entry appear in wp_postmeta using the following code:

    update_field('images','', $post_id);
     update_sub_field(array('images', 1, 'image'), '', $post_id);

    Here images is the repeater field, and image is the subfield

    If I check the database however, the field key doesn’t exist its just an empty string.

    SELECT meta_id, meta_key, meta_value FROM wp_postmeta where post_id=20963 AND meta_key like '%image%'

    You would expect this to return an entry with a field key however it returns this:

    mysql> SELECT meta_id, meta_key, meta_value FROM wp_postmeta where post_id=20963 AND meta_key like '%image%';
    +---------+-----------+------------+
    | meta_id | meta_key  | meta_value |
    +---------+-----------+------------+
    |  140515 | images    |            |
    |  140516 | _images   |            |
    |  140517 | _0_image  |            |
    |  140518 | __0_image |            |
    +---------+-----------+------------+
    4 rows in set (0.00 sec)

    The next step for me is to just do the query manually once I work out how ACF handles storing images in meta_value. This seems like a big limitation for anyone importing data and programatically inserting it into repeater fields, ive spend 2 days trying to find a work around 🙁 – I will work one out and post it here.

  • Ok, I have finally figured it out – this is what tripped me up:

    – Field key isn’t generated until you add something via the GUI, once you do this you can find it in the database, I think the docs mention this.

    – If its a subfield of a repeater field you are adding too, you need 2 keys, the first is for the repeater field, the second is the subfield. So the code to do this looks like:

    update_sub_field(array('field_55fcc711c095d', 1, 'field_55fcc726c095e'),20956, 20967);

    Where 20956 is an attachment id of an image, and 20967 is the post id for testing purposes.

    This however Will not work Unless I add one image manually through the GUI.

    ———— UPDATE ————–

    I worked out a workaround for the page by querying directly:

    select * from wp_postmeta where post_id=$post_id AND meta_key LIKE 'images_%image%'

    This query gets all the fields I need out, and I can retrieve the attachment IDs from it. This works fine for the page – however on the exact same post in the edit screen I cant see the images in the repeater field.

    TL;DR – They are in the DB, I can get them on the page, but they don’t appear on the edit screen and aren’t retrievable using the_repeater_field.

  • Going to be honest, outside of the documentation I’m not really sure. I think that the problem has something to do with inserting post and data during the import. I’ve done this before and importing comes with it’s own problems. Usually the way I do this is to completely skip the ACF functions and either go right to the database or use add_post_meta(). I can’t say if this would be helpful in your case or not.

    Usually, with what you are seeing, being able to get the content on the front end and not seeing them in the back end, is a sign that the field key references are not being inserted properly. So it appears that for some reason the keys are not being updated correctly.

    Again, not sure it this will help you, but it’s an example of how to add post meta values that will be correct.

    
    // one repeater field with one sub field
    $repeater_name = 'repeater';
    $repeater_key = 'field_1234567890123';
    $subfield_name = 'sub_field';
    $subfield_key = 'field_2345678901234';
    $data = array('row 1', 'row_2', 'etc'); // represents your data to insert
    $count = count($data);
    if ($count) {
      add_post_meta($post_id, $repeater, $count, true);
      add_post_meta($post_id, '_'.$repeater, $repeater_key, true);
      for ($i=0; $i<$count; $i++) {
        $meta_key = $repeater.'_'.$i.'_'.$subfield_name;
        add_post_meta($post_id, $meta_key, $data[$i], true);
        add_post_meta($post_id, '_'.$meta_key, $subfield_key, true);
      }
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Adding subfields to Repeater Field’ is closed to new replies.