Support

Account

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

  • 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?