Support

Account

Home Forums Front-end Issues ACF image field and update_field

Solved

ACF image field and update_field

  • Hello friends! I’m with some problems to get a headache with ACF. I have a custom field that store the url of an image. When I try to create the post via code, the field won’t receive the image url, and the field appears blank on post edit in the admin…

    Here is my code:

    
        $post = array(
            'post_title' => $_POST['nomerazao'],
            'post_status' => 'pending',
            'post_content' => $_POST['conteudo'],
            'post_type' => $_POST['tipo-cadastro']
        );
    
        global $post_id;
        $post_id = wp_insert_post($post);
    
        if ($_POST['tipo-cadastro'] == "profissionais") {
            add_post_meta($post_id, 'telefone_1', $_POST['telefone1']);
            add_post_meta($post_id, 'telefone_2', $_POST['telefone2']);
            add_post_meta($post_id, 'telefone_3', $_POST['telefone3']);
            add_post_meta($post_id, 'email', $_POST['email']);
            add_post_meta($post_id, 'endereco', $_POST['endereco']);
            add_post_meta($post_id, 'especialidade', $_POST['especialidades']);
            add_post_meta($post_id, 'formacao', $_POST['formacao']);
            add_post_meta($post_id, 'horario_seg_a_sexta', $_POST['segsex']);
            add_post_meta($post_id, 'horario_sabados', $_POST['sab']);
            add_post_meta($post_id, 'horario_domingos', $_POST['dom']);
        } else if ($_POST['tipo-cadastro'] == "parceiros") {
            add_post_meta($post_id, 'especialidade', $_POST['email']);
            add_post_meta($post_id, 'telefone_1', $_POST['telefone1']);
            add_post_meta($post_id, 'telefone_2', $_POST['telefone2']);
            add_post_meta($post_id, 'telefone_3', $_POST['telefone3']);
            add_post_meta($post_id, 'email', $_POST['email']);
            add_post_meta($post_id, 'endereco', $_POST['endereco']);
        }
    
        //upload da foto
        if(is_uploaded_file($_FILES['foto']['tmp_name'])) {
            $remove_these = array(' ', '

    ‘, ‘”‘, ‘\”, ‘\\’, ‘/’);
    $newname = str_replace($remove_these, ”, $_FILES[‘foto’][‘name’]);
    $newname = time() . ‘-‘ . $newname;

    $uploads = wp_upload_dir();
    $upload_path = “{$uploads[‘path’]}/$newname”;
    move_uploaded_file($_FILES[‘foto’][‘tmp_name’], $upload_path);
    $upload_file_url = “{$uploads[‘url’]}/$newname”;

    $wp_filetype = wp_check_filetype(basename($upload_path), null );
    $attachment = array(
    ‘guid’ => $upload_file_url,
    ‘post_mime_type’ => $wp_filetype[‘type’],
    ‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, basename($upload_path)),
    ‘post_content’ => ”,
    ‘post_status’ => ‘inherit’
    );
    $attach_id = wp_insert_attachment($attachment, $upload_path, $post_id);
    update_post_meta($post_id, ‘foto’, $attach_id);

    require_once(ABSPATH . ‘wp-admin/includes/image.php’);
    $attach_data = wp_generate_attachment_metadata( $attach_id, $upload_path );
    wp_update_attachment_metadata( $attach_id, $attach_data );

    update_field(“field_54777e48c09XX”, $upload_file_url, $post_id);
    }
    `

    I tried checking for the post with get_field(), it returns blank. I tried updating the post after i set the field, and nothing changed…

    Please any help or advice will be great!

Viewing 1 post (of 1 total)

The topic ‘ACF image field and update_field’ is closed to new replies.