Home › Forums › Front-end Issues › Field is not automatically saved using update_field and wp_insert_post
Hi everyone,
Sorry my mistakes in english, but I’m quite concerned about this problem.
When I create a new post using the two functions above and do a query using get_field
that field does not appear.
I must go to the newly created post and tighten to update post so that the field is available in the query.
How should I proceed so that everything runs as reported in the last function found on this page: http://www.advancedcustomfields.com/resources/functions/update_field/ ?
My code:
// Cria um novo Arquivo, se houver dados via $_POST
$my_post = array(
'post_title' => $_POST['file_title'],
'post_content' => $_POST['file_desc'],
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_category' => $cats,
'post_type' => 'arquivo'
);
$post_created = wp_insert_post( $my_post );
// Relaciona o Arquivo criado ao Evento indicado via $_GET
update_field( "evento", $eventoid, $post_created );
I forgot my loop:
<ul>
<?php query_posts(array( 'post_type' => 'arquivo' ) ); ?>
<?php while (have_posts()) : the_post(); ?>
<?php
/*
* Caso o campo Evento informado no Arquivo seja igual o evento
* informado via URL, busca-se todos os arquivos (mídias) a esse Arquivo
*/
$_evento = get_field('evento');
if($_evento != ""){
if($_evento == $eventoid){
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if(count($attachments) > 0 ){
foreach ($attachments as $file){
if ( $last_id = get_post_meta($file->ID, '_edit_last', true) ) {
$last_user = get_userdata($last_id);
}
else{
$last_user = get_userdata($file->post_author);
} ?>
<li>
<?php echo date("d.m.Y", strtotime($file->post_modified)); ?>
<span>Última alteração feira por: <?php echo $last_user->display_name;?></span>
<a href="<?php echo $file->guid?>" title="<?php echo $file->post_title?>" target="_blank"><?php echo $file->post_title?></a>
<p><?php echo $file->post_content;?></p>
</li>
<?php }
}
}
} ?>
<?php endwhile;?>
</ul>
The post title, content… is show but the field ‘evento’ NOT.
Thank´s for replay (what????).
If someone have the same problem must specify the field KEY.
I changed update_field( "evento", $eventoid, $post_created );
for update_field( "field_my_field_key", $eventoid, $post_created );
Hi @roneclay
Yesterday I was unable to respond to support tickets.
Yes, as explained in the update_field documentation, the fiel key must be used instead of the field name (if you do not already have a reference value saved for that post).
Thanks
E
The topic ‘Field is not automatically saved using update_field and wp_insert_post’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.