Skip to content

Instantly share code, notes, and snippets.

@iansvo
Created November 28, 2016 21:33
Show Gist options
  • Save iansvo/0c120b2893e0a62416e4a97ac51e859b to your computer and use it in GitHub Desktop.
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
<?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);
@mizukimadness
Copy link

Worked perfectly! I used $settings['key'] instead of ID, though.

@ejeaglesct
Copy link

I'll admit I'm a noob for some more advance WP editing. Where is this code injected? In the wp_config?

@ian-svoboda-prom
Copy link

@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