Support

Account

Home Forums General Issues Retrive field_key outside the loop

Helping

Retrive field_key outside the loop

  • When a ACF field is inserted in db, appears in wp_postmeta 2 values:

    icon  = 12 (post_id)
    _icon = field_5a6b27a3572f5 (field_key)

    I want to get the field_key for an ACF custom field when i do not have any post inserted in db.
    I run the script outside the loop to import a site with some icons in database.

    $field = get_field_object( 'icon' );
    $field_key = $field['key'];
    echo $field_key;  // this works on a post page, but doesnt work in a custom wp admin template

    Then i insert in db update_field() using the exact field_key

    I must declare a global variable or is another way to get key field?

  • As far as I know there isn’t a way to get a field object by the field name when there is nothing already in the database for that field.

    Instead of using the field name in get_field_object, use the field key, or in this case, just hard code the field key for that field.

    In ACF, multiple fields can have the same name. ACF does not know which field to get unless it can get the key form the database for whatever object the field is attached to, like a post or term or options.

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

The topic ‘Retrive field_key outside the loop’ is closed to new replies.