Home › Forums › Front-end Issues › 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!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
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.