Last active
December 1, 2019 17:27
-
-
Save francescocarlucci/e7377937ee831200e52733fc1f449e5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('ninja_forms_render_options','ninja_forms_pre_population_callback', 10, 2); | |
function ninja_forms_pre_population_callback($options, $settings) { | |
// target only the field with this key | |
if( $settings['key'] == 'my_field_key' ) { | |
// write the query to fetch the data | |
$args = array( | |
'post_type' => 'post', | |
'post_status' => 'publish' | |
); | |
$posts = new WP_Query( $args ); | |
if ( $posts->have_posts() ) { | |
$options = array(); | |
while ( $posts->have_posts() ) { | |
$posts->the_post(); | |
// $options is the variable which contains tha values rendered | |
// we will use the post title as lable and the ID as value | |
$options[] = array( | |
'label' => get_the_title(), | |
'value' => get_the_ID() | |
); | |
} // endwhile | |
} // endif | |
wp_reset_postdata(); | |
} | |
return $options; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So I have two select fields in my Ninja Form. Please note that my google sheet is a list of products and corresponding brands. First one displays a list of brands from the google sheet and the second one needs to display the products based on the brand selected. I have the Conditional Logic add-on installed but I am not sure how to get the selected brand in the functions.php file without the need to submit the form.