Skip to content

Instantly share code, notes, and snippets.

@bryanwillis
Created March 27, 2019 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanwillis/c79ccee26a756de5af623eb2caaa3fad to your computer and use it in GitHub Desktop.
Save bryanwillis/c79ccee26a756de5af623eb2caaa3fad to your computer and use it in GitHub Desktop.
add_filter(frm_setup_new_fields_vars, frm_populate_member, 20, 2);
add_filter(frm_setup_edit_fields_vars, frm_populate_member, 20, 2); //use this function on edit too
function frm_populate_member($values, $field){
if($field->id == 732 || $field->id == 733){ //replace 732 and 733 with the IDs of the fields to populate
global $wpdb;
//the Query
$the_Result = $wpdb->get_results( "SELECT concat_ws(', ', last_name, first_name) as display_name FROM wp_database.wp_wp_eMember_members_tbl where account_state = 'active' order by last_name, first_name;" );
unset($values['options']); //break the binding of any existing content in the values array
$values['options'] = array(); //remove this line if you are using a checkbox or radio button field
$values['options'][]="";
//the Loop
if(! empty($the_Result)){
foreach($the_Result as $a_user){
$values['options'][]=$a_user->display_name;
$values['use_key'] = false;
}
}
//$values['use_key'] = true; //this will set the field to save the post ID instead of post title
}
return $values;
}
add_filter( 'gform_pre_render_51', 'populate_posts' );
add_filter( 'gform_pre_validation_51', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_51', 'populate_posts' );
add_filter( 'gform_admin_pre_render_51', 'populate_posts' );
function populate_posts( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
$posts = get_posts( 'numberposts=-1&post_status=publish' );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a Post';
$field->choices = $choices;
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment