Home Forums Bug Reports get_fields() does not return all values


get_fields() does not return all values

  • Hello,
    i have a problem with the get_fields() function

    1) i created a normal textfield
    2) i set the value with php
    update_field('setCard_kontakt_bezirk', 'TestONE', $setCardId);

    // this works
    get_field('setCard_kontakt_bezirk',$setCardId, false); // returns TestONE

    // this works
    acf_form( array( ... ) ) ) ... // return the form with the value TestONE

    But when i use get_fields($setCardId)

    $sc = get_fields($setCardId);
    $sc['setCard_kontakt_bezirk']  // undefined

    When i use the field ID to set the value
    update_field('field_57a2c771f3661', 'TestONE', $setCardId);

    it works, i get the value with the get_fields($setCardId) function.

    Any ideas whats wrong?

  • Hi @manu

    ACF saves two entries in the database, the value and the reference. The value will look like this in the database:

    field_name : field-value

    While the reference looks like this:

    _field_name : field_1234567890abc

    The reference is used by the get_field() function to find the right value for the right field name. That’s because you can have two fields with the same name, but not the same field key.

    When you execute this code:

    update_field('setCard_kontakt_bezirk', 'TestONE', $setCardId);

    ACF doesn’t know which field you want to update exactly (remember that you can have two fields with the same name), so it will only record the value entry.

    When you execute this code:

    update_field('field_57a2c771f3661', 'TestONE', $setCardId);

    ACF know which one you want to update, so it can create the reference key that is needed by the get_field() function.

    If the reference entry is recorded before (e.g. by saving the post from backend), it’s safe to use the update_field() function with the field name. But if it’s a new entry, you should use the field key instead.

    I hope this make sense 🙂

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

The topic ‘get_fields() does not return all values’ is closed to new replies.