Skip to content

Instantly share code, notes, and snippets.

@machouinard
Created October 24, 2016 19:24
Show Gist options
  • Save machouinard/e164aa98cf6febff2ffda586b3dc2c3c to your computer and use it in GitHub Desktop.
Save machouinard/e164aa98cf6febff2ffda586b3dc2c3c to your computer and use it in GitHub Desktop.
add_filter( 'gform_pre_render_9', 'uri_interested_property' );
add_filter( 'gform_pre_validation_9', 'uri_interested_property' );
add_filter( 'gform_pre_submission_9', 'uri_interested_property' );
add_filter( 'gform_admin_pre_render_9', 'uri_interested_property' );
/**
* Dynamically builds Gravity Fields dropdown for Property of Interest form field
*
* @param $form
*
* @return mixed
*/
function uri_interested_property( $form ) {
foreach ( $form['fields'] as &$field ) {
//* Only interested in dropdowns with the 'uri_interested_property' class
if ( $field->type != 'select' || strpos( $field->cssClass, 'uri_interested_property' ) === false ) {
continue;
}
global $wpdb;
//* Select property addresses from post meta
$sql = "SELECT DISTINCT meta_value as address FROM wp_postmeta m, wp_posts p WHERE meta_key = '_zoner_address' && p.ID = m.post_id && p.post_status = 'publish' ORDER BY meta_value;";
$posts = $wpdb->get_results( $sql );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->address, 'value' => $post->address );
}
$property_choices = array_map("unserialize", array_unique(array_map("serialize", $choices ) ) );
$field->placeholder = 'Select a Property';
$field->choices = $property_choices;
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment