Support

Account

Home Forums Bug Reports Programmatically inserting field with update_field() does not at _field_key meta

Solved

Programmatically inserting field with update_field() does not at _field_key meta

  • If you programmatically create a post with wp_insert_post() then do something like this:

    
    update_field( 'field_group_summary', $some_html, $some_new_post_id );
    

    You have to manually add the field key meta

    
    update_post_meta( $some_new_post_id, '_field_group_summary', 'field_59c383916102a' );
    

    Or doing

    
    $settings = get_field( 'field_group' );
    

    Will return different results than when saved via the WordPress UI.

  • You can insert the value correctly by using the field key

    
    update_field( 'field_59c383916102a', $some_html, $some_new_post_id );
    

    This is explained in the documentation https://www.advancedcustomfields.com/resources/update_field/

  • Thanks, apologies for not noticing that caveat about saving a new value vs updating existing!

  • Not a problem, a lot of people overlook it. It’s probably the number 1 question asked about the update_field() question. I had problems with it myself when I first starting using it.

  • Unfortunately this doesn’t actually seem solve it when I test it out.

    If you create a field group settings, add a field to it my_settings, when saving via the UI you get a wp_postmeta of settings_my_settings with a field key like field_59c383916102a.

    If you create it programatically via your recommendation:

    
    update_field( 'field_59c383916102a', $some_html, $some_new_post_id );
    

    In the database you instead get my_settings (when settings_my_settings is needed). This causes then field to be empty when then opening it up in the UI.

  • ACF does not append the field group name to the field name when adding it to the database.

    Is this some type of nested field? a nested repeater or a clone field or do you have some other kind of more complicated fields than just a field in a group?

  • Hi John,
    Thanks for your continued help with this.

    ACF does not append the field group name to the field name when adding it to the database.

    It’s prepend actually, but essentially the above doesn’t appear correct. Please see the attached screenshots of a sample ACF Config in ACF Pro latest, the text added via Edit Post screen, and the resulting database values.

    Thanks,
    Scott

  • You are talking about a “Group Field” and not a “Field Group”.

    A “Group Field” in all aspects is a repeater field that always has exactly 1 row. Updating a group field would follow the same rules as updating a repeater or a sub field.

    This is why trying to update the “sub field” is not working correctly when using update_field().

    You need to look at the sections here about working with repeaters and sub fields.

    This can be done by providing the entire group field array in the update, for example.

    
    $value = array(
      // nested array for row
      // field key => value pairs
      'field_123456' => 'sub field 1 value',
      'field_654321' => 'sub field 2 value',
      // etc
    );
    // update the "group field"
    update_field('field_000000', $value);
    
  • Thanks!

  • Hello.

    I have a similar problem with ACFPro (but not with subfield): when adding a custom post type with wp_insert_post() (with status “PENDING”!) and then calling multiple

    update_field('field_key','value', $post_id).

    I have a ‘transition_post_status’ hook that sends email when post just became “new” -> “pending”. But here,
    get_field('field_key', $ID);
    returns empty string (even if I use field id instead of key)!

    Any ideas / suggestions?

    Thanks!

  • update_post() is not an ACF function. Are you sure that’s what you’re calling? This should be creating a fatal error, unless this function happens to be defined in some other plugin.

    
    update_field('field_key','value', $post_id);
    

    It also depends on what type of field you are updating and what you’re trying to set the value to.

  • Hey!

    Typo, sorry. It’s update_field() of course and I’m setting basic text field value, so it’s a string.

    Really at a loss here after a few hours of digging around 🙂

    So, to write it down further (perhaps I find something odd now that I write it for someone else):

    $email = get_field($fields['org_email'], $ID); // $ID here is OK (printed in email that is sent)

    $fields is and array with field_id => field_key value pairs. This also seems to be working because a new post is added with the data via update_field() functions.

    And post is initially added like this:

    $post_id = wp_insert_post(
       array (
           'post_type' => 'my_post_type',
           'post_title' => 'Dynamic Name here',
           'post_status' => 'pending',
       )
    );
    
    // check if basic insertion was successful
    if( ! is_numeric($post_id)) {
        return false;
    } else { 
            
    $fields = lpml_get_acf_fields_key_name_map(); // this is field id => key value pair array
    
    update_field($fields['org_email'], $params_array['org_email'], $post_id); 

    This saves the data properly and all, except when I want to get the data via

    transition_post_status

    action it’s empty (empty string it seems). Is it to early to get this data here?

    When using ‘pending_to_publish’ hook for example, data is available …

    Is it possible, that values are not properly received because the post prior to this was still “pending” ? When I call update_field() the post was first inserted via wp_insert_post with the “status” => “pending”.

  • As long as this field is not a sub field of a repeater, flex field, clone or group field, it should be working if the field key and post ID is correct.

    I have had issues with updating fields due to the WP post meta cache. Sometimes you need to clear it before trying to update.

    
    wp_cache_delete($post_id, 'post_meta');
    
  • Thanks,

    but unfortunately this didn’t help. ID is correct, field is a basic top-level text field and field key is legit as well!

    wp_cache_delete() shouldn’t solve anything anyway when adding a new post right?

  • Same here, i use update_field(‘total_de_capitulos’,$capitulos,’livro_’.$idLivro);

    If i go to wp-admin everything is ok

    But when i try
    $termo = get_queried_object();
    echo get_field(‘total_de_capitulos’, $termo);

    I get wrong results.

    And i checked that if i go to the term and save it again everything works fine!

  • Hi Guys,

    can someone hep me do a simple Button or updating a field by unfocusing the textarea?

    update_field

  • Hi,

    Anyone found a solution? I’ve the same problem, when I update field after create or update WP Post, I have to resave manually the WP Post to have correct data in get_field(), else I have wrong data display in front-end… :'(

  • Apparently I found a solution:

    Just add do_action('acf/save_post', $postID); at the end of the script, and that’s all…

  • I’ve tried that and it doesn’t make any difference to me; It looks like the meta fields aren’t all set up unless you load the form in wp-admin then save it. I’m trying to find someway to replicate this entirely in code; Feels like need to replicate the setup when the form loads not just the save, but can’t get it.

    Was there anything else you were doing in your function?
    I create a post; update a couple of fields (some simple text, one a repeater) then call the acf/save_post action but there’s still nothing there when I pull back the post meta, until I load and save via the wp-admin user interface.

    I read a post elsewhere saying that they were creating the JSON for the field structure and posting that but I’m not sure how to go about getting hold of that.

  • As I stated before, when inserting data you must use the field keys, field names will not work where there is not already existing data saved to the field, which is what happens when you’re adding new posts.

    For top level field (not in a group, repeater, flex field)

    
    update_field('field_XYZ123', $value, $post_id)
    

    for a repeater or a flex field

    
    $value = array(
      // each element of this array is a row in the repeater
      array(
        // each element of this row is a field key => value pair
        'field_XXXXXXX' => 'value',
        'field_YYYYYYY' => 'value'
      )
    );
    // field_ZZZZZZZ is the repeater field key
    update_field('field_ZZZZZZZ', $value, $post_id);
    

    for a group field

    
    $value = array(
      // a group field is a repeater with a single row
      // there is no nesting required for each row because of this
      // the elements of this array are sub field key => value pairs
      'field_XXXXXXX' => 'value',
      'field_YYYYYYY' => 'value'
    );
    // field_ZZZZZZZ is the group field key
    update_field('field_ZZZZZZZ', $value, $post_id);
    

    If you have nested repeater, group and flex fields then you must create the nesting when updating the fields. It may be possible to use add_row() and update_sub_field(), but these would have the same restrictions of needing to use the field key for new data and it’s generally easier to just construct the arrays that are needed. But then again, I don’t generally insert data this way. If I’m inserting data it’s because I am building an import and I use WP All Import Pro with the ACF add on so that I don’t need to worry about these details.

    The comment I made about deleting the cache is only relevant if you update a field and then want to get the updated value during the same page load unless you have some type of persistent cache installed that caches data across page loads.

  • Hi John, thanks for your answer!

    I use only field keys and I use several levels of nested fields, for example:

    const ACF_ID_COMPANY_IDENTITY = 'field_5d5e603dbfb7b';
    const ACF_ID_COMPANY_IDENTITY_LOGO = 'field_5d5e60cbbfb7d';
    const ACF_ID_COMPANY_IDENTITY_CREATION_YEAR = 'field_5d5e6279bfb80';
    const ACF_ID_COMPANY_IDENTITY_HR_MAIL = 'field_5e022ff49af2b';
    const ACF_ID_COMPANY_IDENTITY_TURNOVER = 'field_5d5e6c44bfb88';
    const ACF_ID_COMPANY_IDENTITY_HEADER_ILLU = 'field_5d5e612abfb7e';
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU = 'field_5d5e61e3bfb7f';
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA = 'field_5dc95f49358a6' . '_' . ACF_ID_GLOBAL_MEDIA;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE = ACF_ID_GLOBAL_MEDIA_TYPE;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_IMAGE = ACF_ID_GLOBAL_MEDIA_IMAGE;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_VIDEO = ACF_ID_GLOBAL_MEDIA_VIDEO;
    
    // Logo
    if ($company->hasLogo() && $company->getLogo()->hasId()) {
        $identity[ACF_ID_COMPANY_IDENTITY_LOGO] = $company->getLogo()->getId();
    }
    
    // Creation Year
    if ($company->hasCreationYear()) {
        $identity[ACF_ID_COMPANY_IDENTITY_CREATION_YEAR] = $company->getCreationYear();
    }
    
    // Global HR Mail Address
    if ($company->hasGlobalHrMailAddress()) {
        $identity[ACF_ID_COMPANY_IDENTITY_HR_MAIL] = $company->getGlobalHrMailAddress();
    }
    
    // Turnover
    if ($company->hasTurnover()) {
        $identity[ACF_ID_COMPANY_IDENTITY_TURNOVER] = $company->getTurnover();
    }
    
    // -- /General
    
    // -- Illustrations
    // Header Illustration
    if ($company->hasHeaderMedia() && $company->getHeaderMedia()->hasId()) {
        $identity[ACF_ID_COMPANY_IDENTITY_HEADER_ILLU] = $company->getHeaderMedia()->getId();
    }
    
    // Main Illustration
    if ($company->hasMainMedia() && ($company->getMainMedia()->hasId() || $company->getMainMedia()->hasSrc())) {
        $mainMedia = [];
    
        if ($company->getMainMedia()->isImage()) {
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE] = Media::ACF_IS_IMAGE;
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_IMAGE] = $company->getMainMedia()->getId();
        }
    
        if ($company->getMainMedia()->isVideo()) {
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE] = Media::ACF_IS_VIDEO;
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_VIDEO] = $company->getMainMedia()->getSrc();
        }
    
        $identity[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU][ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA] = $mainMedia;
    }
    // -- /Illustrations
    
    // Update IDENTITY
    if (!empty($identity)) {
        update_field(ACF_ID_COMPANY_IDENTITY, $identity, $postId);
    }

    So here what do you think I should do? Use add_row() instead of arrays? Use update_sub_field() instead of update_field() on all $identity variable?

    I admit that I don’t understand why this is a problem… Is it really impossible to take the problem the other way by forcing ACF to retrieve the data in the database that was just put in? Because they are present in the database, these data, and we can see them clearly in the backoffice, why we don’t retrieved them in the frontoffice via get_field()? I do not understand… :/

  • Hi John, thanks for your answer!

    I use only field keys and I use several levels of nested fields, for example:

    const ACF_ID_COMPANY_IDENTITY = 'field_5d5e603dbfb7b';
    const ACF_ID_COMPANY_IDENTITY_LOGO = 'field_5d5e60cbbfb7d';
    const ACF_ID_COMPANY_IDENTITY_CREATION_YEAR = 'field_5d5e6279bfb80';
    const ACF_ID_COMPANY_IDENTITY_HR_MAIL = 'field_5e022ff49af2b';
    const ACF_ID_COMPANY_IDENTITY_TURNOVER = 'field_5d5e6c44bfb88';
    const ACF_ID_COMPANY_IDENTITY_HEADER_ILLU = 'field_5d5e612abfb7e';
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU = 'field_5d5e61e3bfb7f';
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA = 'field_5dc95f49358a6' . '_' . ACF_ID_GLOBAL_MEDIA;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE = ACF_ID_GLOBAL_MEDIA_TYPE;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_IMAGE = ACF_ID_GLOBAL_MEDIA_IMAGE;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_VIDEO = ACF_ID_GLOBAL_MEDIA_VIDEO;
    
    // Logo
    if ($company->hasLogo() && $company->getLogo()->hasId()) {
        $identity[ACF_ID_COMPANY_IDENTITY_LOGO] = $company->getLogo()->getId();
    }
    
    // Creation Year
    if ($company->hasCreationYear()) {
        $identity[ACF_ID_COMPANY_IDENTITY_CREATION_YEAR] = $company->getCreationYear();
    }
    
    // Global HR Mail Address
    if ($company->hasGlobalHrMailAddress()) {
        $identity[ACF_ID_COMPANY_IDENTITY_HR_MAIL] = $company->getGlobalHrMailAddress();
    }
    
    // Turnover
    if ($company->hasTurnover()) {
        $identity[ACF_ID_COMPANY_IDENTITY_TURNOVER] = $company->getTurnover();
    }
    
    // -- /General
    
    // -- Illustrations
    // Header Illustration
    if ($company->hasHeaderMedia() && $company->getHeaderMedia()->hasId()) {
        $identity[ACF_ID_COMPANY_IDENTITY_HEADER_ILLU] = $company->getHeaderMedia()->getId();
    }
    
    // Main Illustration
    if ($company->hasMainMedia() && ($company->getMainMedia()->hasId() || $company->getMainMedia()->hasSrc())) {
        $mainMedia = [];
    
        if ($company->getMainMedia()->isImage()) {
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE] = Media::ACF_IS_IMAGE;
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_IMAGE] = $company->getMainMedia()->getId();
        }
    
        if ($company->getMainMedia()->isVideo()) {
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE] = Media::ACF_IS_VIDEO;
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_VIDEO] = $company->getMainMedia()->getSrc();
        }
    
        $identity[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU][ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA] = $mainMedia;
    }
    // -- /Illustrations
    
    // Update IDENTITY
    if (!empty($identity)) {
        update_field(ACF_ID_COMPANY_IDENTITY, $identity, $postId);
    }

    So here what do you think I should do? Use add_row() instead of arrays? Use update_sub_field() instead of update_field() on all $identity variable?

    I admit that I don’t understand why this is a problem… Is it really impossible to take the problem the other way by forcing ACF to retrieve the data in the database that was just put in? Because they are present in the database, these data, and we can see them clearly in the backoffice, why we don’t retrieved them in the frontoffice via get_field()? I do not understand… :/

  • Hi John, thanks for your answer!

    I use only field keys and I use several levels of nested fields, for example:

    `
    const ACF_ID_COMPANY_IDENTITY = ‘field_5d5e603dbfb7b’;
    const ACF_ID_COMPANY_IDENTITY_LOGO = ‘field_5d5e60cbbfb7d’;
    const ACF_ID_COMPANY_IDENTITY_CREATION_YEAR = ‘field_5d5e6279bfb80’;
    const ACF_ID_COMPANY_IDENTITY_HR_MAIL = ‘field_5e022ff49af2b’;
    const ACF_ID_COMPANY_IDENTITY_TURNOVER = ‘field_5d5e6c44bfb88’;
    const ACF_ID_COMPANY_IDENTITY_HEADER_ILLU = ‘field_5d5e612abfb7e’;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU = ‘field_5d5e61e3bfb7f’;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA = ‘field_5dc95f49358a6’ . ‘_’ . ACF_ID_GLOBAL_MEDIA;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE = ACF_ID_GLOBAL_MEDIA_TYPE;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_IMAGE = ACF_ID_GLOBAL_MEDIA_IMAGE;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_VIDEO = ACF_ID_GLOBAL_MEDIA_VIDEO;

    // Logo
    if ($company->hasLogo() && $company->getLogo()->hasId()) {
    $identity[ACF_ID_COMPANY_IDENTITY_LOGO] = $company->getLogo()->getId();
    }

    // Creation Year
    if ($company->hasCreationYear()) {
    $identity[ACF_ID_COMPANY_IDENTITY_CREATION_YEAR] = $company->getCreationYear();
    }

    // Global HR Mail Address
    if ($company->hasGlobalHrMailAddress()) {
    $identity[ACF_ID_COMPANY_IDENTITY_HR_MAIL] = $company->getGlobalHrMailAddress();
    }

    // Turnover
    if ($company->hasTurnover()) {
    $identity[ACF_ID_COMPANY_IDENTITY_TURNOVER] = $company->getTurnover();
    }

    // — /General

    // — Illustrations
    // Header Illustration
    if ($company->hasHeaderMedia() && $company->getHeaderMedia()->hasId()) {
    $identity[ACF_ID_COMPANY_IDENTITY_HEADER_ILLU] = $company->getHeaderMedia()->getId();
    }

    // Main Illustration
    if ($company->hasMainMedia() && ($company->getMainMedia()->hasId() || $company->getMainMedia()->hasSrc())) {
    $mainMedia = [];

    if ($company->getMainMedia()->isImage()) {
    $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE] = Media::ACF_IS_IMAGE;
    $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_IMAGE] = $company->getMainMedia()->getId();
    }

    if ($company->getMainMedia()->isVideo()) {
    $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE] = Media::ACF_IS_VIDEO;
    $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_VIDEO] = $company->getMainMedia()->getSrc();
    }

    $identity[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU][ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA] = $mainMedia;
    }
    // — /Illustrations

    // Update IDENTITY
    if (!empty($identity)) {
    update_field(ACF_ID_COMPANY_IDENTITY, $identity, $postId);
    }
    `

    So here what do you think I should do? Use add_row() instead of arrays? Use update_sub_field() instead of update_field() on all $identity variable?

    I admit that I don’t understand why this is a problem… Is it really impossible to take the problem the other way by forcing ACF to retrieve the data in the database that was just put in? Because they are present in the database, these data, and we can see them clearly in the backoffice, why we don’t retrieved them in the frontoffice via get_field()? I do not understand… :/

  • Hi John, thanks for your answer!

    I use only field keys and I use several levels of nested fields, for example:

    `
    const ACF_ID_COMPANY_IDENTITY = ‘field_5d5e603dbfb7b’;
    const ACF_ID_COMPANY_IDENTITY_LOGO = ‘field_5d5e60cbbfb7d’;
    const ACF_ID_COMPANY_IDENTITY_CREATION_YEAR = ‘field_5d5e6279bfb80’;
    const ACF_ID_COMPANY_IDENTITY_HR_MAIL = ‘field_5e022ff49af2b’;
    const ACF_ID_COMPANY_IDENTITY_TURNOVER = ‘field_5d5e6c44bfb88’;
    const ACF_ID_COMPANY_IDENTITY_HEADER_ILLU = ‘field_5d5e612abfb7e’;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU = ‘field_5d5e61e3bfb7f’;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA = ‘field_5dc95f49358a6’ . ‘_’ . ACF_ID_GLOBAL_MEDIA;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE = ACF_ID_GLOBAL_MEDIA_TYPE;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_IMAGE = ACF_ID_GLOBAL_MEDIA_IMAGE;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_VIDEO = ACF_ID_GLOBAL_MEDIA_VIDEO;

    // Logo
    if ($company->hasLogo() && $company->getLogo()->hasId()) {
    $identity[ACF_ID_COMPANY_IDENTITY_LOGO] = $company->getLogo()->getId();
    }

    // Creation Year
    if ($company->hasCreationYear()) {
    $identity[ACF_ID_COMPANY_IDENTITY_CREATION_YEAR] = $company->getCreationYear();
    }

    // Global HR Mail Address
    if ($company->hasGlobalHrMailAddress()) {
    $identity[ACF_ID_COMPANY_IDENTITY_HR_MAIL] = $company->getGlobalHrMailAddress();
    }

    // Turnover
    if ($company->hasTurnover()) {
    $identity[ACF_ID_COMPANY_IDENTITY_TURNOVER] = $company->getTurnover();
    }

    // — /General

    // — Illustrations
    // Header Illustration
    if ($company->hasHeaderMedia() && $company->getHeaderMedia()->hasId()) {
    $identity[ACF_ID_COMPANY_IDENTITY_HEADER_ILLU] = $company->getHeaderMedia()->getId();
    }

    // Main Illustration
    if ($company->hasMainMedia() && ($company->getMainMedia()->hasId() || $company->getMainMedia()->hasSrc())) {
    $mainMedia = [];

    if ($company->getMainMedia()->isImage()) {
    $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE] = Media::ACF_IS_IMAGE;
    $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_IMAGE] = $company->getMainMedia()->getId();
    }

    if ($company->getMainMedia()->isVideo()) {
    $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE] = Media::ACF_IS_VIDEO;
    $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_VIDEO] = $company->getMainMedia()->getSrc();
    }

    $identity[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU][ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA] = $mainMedia;
    }
    // — /Illustrations

    // Update IDENTITY
    if (!empty($identity)) {
    update_field(ACF_ID_COMPANY_IDENTITY, $identity, $postId);
    }
    `

    So here what do you think I should do? Use add_row() instead of arrays? Use update_sub_field() instead of update_field() on all $identity variable?

    I admit that I don’t understand why this is a problem… Is it really impossible to take the problem the other way by forcing ACF to retrieve the data in the database that was just put in? Because they are present in the database, these data, and we can see them clearly in the backoffice, why we don’t retrieved them in the frontoffice via get_field()? I do not understand… :/

  • Hi John, thanks for your answer!

    I use only field keys and I use several levels of nested fields, for example:

    
    const ACF_ID_COMPANY_IDENTITY = 'field_5d5e603dbfb7b';
    const ACF_ID_COMPANY_IDENTITY_LOGO = 'field_5d5e60cbbfb7d';
    const ACF_ID_COMPANY_IDENTITY_CREATION_YEAR = 'field_5d5e6279bfb80';
    const ACF_ID_COMPANY_IDENTITY_HR_MAIL = 'field_5e022ff49af2b';
    const ACF_ID_COMPANY_IDENTITY_TURNOVER = 'field_5d5e6c44bfb88';
    const ACF_ID_COMPANY_IDENTITY_HEADER_ILLU = 'field_5d5e612abfb7e';
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU = 'field_5d5e61e3bfb7f';
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA = 'field_5dc95f49358a6' . '_' . ACF_ID_GLOBAL_MEDIA;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE = ACF_ID_GLOBAL_MEDIA_TYPE;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_IMAGE = ACF_ID_GLOBAL_MEDIA_IMAGE;
    const ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_VIDEO = ACF_ID_GLOBAL_MEDIA_VIDEO;
    
    // Logo
    if ($company->hasLogo() && $company->getLogo()->hasId()) {
        $identity[ACF_ID_COMPANY_IDENTITY_LOGO] = $company->getLogo()->getId();
    }
    
    // Creation Year
    if ($company->hasCreationYear()) {
        $identity[ACF_ID_COMPANY_IDENTITY_CREATION_YEAR] = $company->getCreationYear();
    }
    
    // Global HR Mail Address
    if ($company->hasGlobalHrMailAddress()) {
        $identity[ACF_ID_COMPANY_IDENTITY_HR_MAIL] = $company->getGlobalHrMailAddress();
    }
    
    // Turnover
    if ($company->hasTurnover()) {
        $identity[ACF_ID_COMPANY_IDENTITY_TURNOVER] = $company->getTurnover();
    }
    
    // -- /General
    
    // -- Illustrations
    // Header Illustration
    if ($company->hasHeaderMedia() && $company->getHeaderMedia()->hasId()) {
        $identity[ACF_ID_COMPANY_IDENTITY_HEADER_ILLU] = $company->getHeaderMedia()->getId();
    }
    
    // Main Illustration
    if ($company->hasMainMedia() && ($company->getMainMedia()->hasId() || $company->getMainMedia()->hasSrc())) {
        $mainMedia = [];
    
        if ($company->getMainMedia()->isImage()) {
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_TYPE] = Media::ACF_IS_IMAGE;
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_IMAGE] = $company->getMainMedia()->getId();
        }
    
        if ($company->getMainMedia()->isVideo()) {
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_ TYPE] = Media::ACF_IS_VIDEO;
            $mainMedia[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA_VIDEO] = $company->getMainMedia()->getSrc();
        }
    
        $identity[ACF_ID_COMPANY_IDENTITY_MAIN_ILLU][ACF_ID_COMPANY_IDENTITY_MAIN_ILLU_MEDIA] = $mainMedia;
    }
    // -- /Illustrations
    
    // Update IDENTITY
    if (!empty($identity)) {
        update_field(ACF_ID_COMPANY_IDENTITY, $identity, $postId);
    }
    

    So here what do you think I should do? Use add_row() instead of arrays? Use update_sub_field() instead of update_field() on all $identity variable?

Viewing 25 posts - 1 through 25 (of 31 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.