dropdown autopopulate with separate values

By: Roberta Cerami | Asked: 03/21/2024
ForumsCategory: Code Helpdropdown autopopulate with separate values
Roberta Cerami asked 4 weeks ago

Hi,  I need a dropdown field with autopopulate value from list of employees. I have it already and it works but I need also get the email. I have the name list but after they make a choice the system by the post id need to retreive their email(ACF field ) for the one selected and use it to send him a confirmation email. How can I return another values?Any ideas?    That's the code  add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_populate_posts', 20, 2); //use this function on edit too
function frm_populate_posts($values, $field){
if($field->id == 61){ //replace 125 with the ID of the field to populate
$posts = get_posts( array('post_type' => 'employee', 'post_status' => array('publish', 'private'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC'));
unset($values['options']);
$values['options'] = array(''); //remove this line if you are using a checkbox or radio button field
$values['options'][''] = ''; //remove this line if you are using a checkbox or radio button field
foreach($posts as $p){
$values['options'][$p->ID] = $p->post_title;
}
$values['use_key'] = false; //this will set the field to save the post ID instead of post title
unset($values['options'][0]);
}
return $values;
}

1 Answers
Roberta Cerami answered 4 weeks ago
1
add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2);
2
add_filter('frm_setup_edit_fields_vars', 'frm_populate_posts', 20, 2); //use this function on edit too
3  
function frm_populate_posts($values, $field){
4  
  if($field->id == 61){ //replace 125 with the ID of the field to populate
5
    $posts = get_posts( array('post_type' => 'employee', 'post_status' => array('publish', 'private'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC'));
6  
    unset($values['options']);
7  
    $values['options'] = array(''); //remove this line if you are using a checkbox or radio button field
8  
    $values['options'][''] = ''; //remove this line if you are using a checkbox or radio button field
9  
    foreach($posts as $p){
10
     // $values['options'][$p->ID] = $p->post_title;
11
     $email= get_field("email",$p->ID);
12
         $values['options'][] = array(
13
                 'label' => $p->post_title,
    14
                 'value' => $email
15
             );
16
    }
17  
    $values['use_key'] = false; //this will set the field to save the post ID instead of post title
18  
    unset($values['options'][0]);
19
  }
20
  return $values;
21
}
22
23


That's the solution, selecting "use separate values" in the dropdown.
        ?                                                

Scope

 

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right