Home › Forums › Front-end Issues › Retrive data from multiple users from user field type
Hello.
I would like to retrive users emails durning creating new post with ACF form (ver non premium). I can retrive one single address in other setup but I have problem to do it with many users.
function my_pre_save_post( $post_id )
{
$current_user = wp_get_current_user();
// Create a new post
$post = array(
'post_status' => 'publish' ,
'post_title' => $_POST['fields']['field_57baf30ad86ab'],
'post_type' => 'message',
);
// insert the post
$post_id =wp_insert_post( $post );
$test = get_the_title($post_id);
$data= get_field('recipient',$post_id); // trying to get access recipents of post im currently creating
$mail_array= array();
for ($i = 0; $i <= 3; $i++) {
if ($data[$i][user_email]) {
array_push($mail_array, $data[$i][user_email]); //for making string with addreses
}
}
$mails = implode(',', $mail_array);
update_field('test', $mails, 23); // to checking how data looks
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
?>
<?php
I noticed that if I update that test field with get_the_title($post_id); there is no problem and also if I use $data= get_field(‘recipient’,$post_id-1); the loop is working, so its like the data of custom fields is not ready durning this process.
Maybe the path that I took is wrong and maybe there is more effective way.
I just want to get email addresses of users that I marked in ACF form of user type field, and prepare it to mail() php function.
The post and the custom field on the post do not exist yet. pre_save_post
fires before ACF has added the content to the field. To access the value that are submitted you need to look in $_POST['acf']
at this point. The other choice is to add and acf/save_post
action that will fire after ACF has saved the values.
Thank you very much!
I used 2nd suggestion and it works like it should to because that $_POST[‘acf’] worked only for one address but it doesnt matter why it wasnt working for multiple addreses.
Im really grateful because I would not be able to solve this alone I think. Once again, thanks!
The topic ‘Retrive data from multiple users from user field type’ 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.