Skip to content

Instantly share code, notes, and snippets.

@iansvo
Created June 6, 2016 00:41
Show Gist options
  • Save iansvo/8138934d7de15e07270bf915be833e92 to your computer and use it in GitHub Desktop.
Save iansvo/8138934d7de15e07270bf915be833e92 to your computer and use it in GitHub Desktop.
Populate Ninja Forms List with Custom Post Types
<?php
function locations_prepopulate_forms($data, $field_id) {
global $post;
// Populates a specific list (by field ID) with the "locations" cpt's
if($field_id == 27) {
$args = array(
'post_type' => 'location',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$data['list']['options']=array();
while ( $query->have_posts() ) {
$query->the_post();
$data['list']['options'][] = array(
'label' => get_the_title(),
'value' => $post->post_name,
'calc' => null,
'selected' => 0
);
}
}
wp_reset_postdata();
}
return $data;
}
add_filter('ninja_forms_field','locations_prepopulate_forms', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment