Created
November 28, 2016 21:33
-
-
Save iansvo/0c120b2893e0a62416e4a97ac51e859b to your computer and use it in GitHub Desktop.
In WordPress, use to pre-populate a Ninja Forms select list with options, using Custom Post Types
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
<?php | |
function cpt_prepopulate_forms($options, $settings) { | |
global $post; | |
if( $settings['id'] == 27 ) // change to your field ID | |
{ | |
$args = array( | |
'post_type' => 'location', // Change to your Custom Post type | |
); | |
$query = new WP_Query( $args ); | |
if ( $query->have_posts() ) | |
{ | |
$options = array(); | |
while ( $query->have_posts() ) | |
{ | |
$query->the_post(); | |
$options[] = array( | |
'label' => get_the_title(), | |
'value' => $post->post_name, | |
'calc' => null, | |
'selected' => 0 | |
); | |
} | |
} | |
wp_reset_postdata(); | |
} | |
return $options; | |
} | |
add_filter('ninja_forms_render_options','cpt_prepopulate_forms', 10, 2); |
I'll admit I'm a noob for some more advance WP editing. Where is this code injected? In the wp_config?
@ejeaglesct This could go inside your theme's functions.php file or any php file that is included/required by your functions.php file.
I'm glad to see you all are getting value after this (I wrote this forever ago). I hope that helps!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked perfectly! I used
$settings['key']
instead of ID, though.