Created
October 9, 2019 07:14
-
-
Save erlendmr/6158f6596afbeae6de3ff9d61bd2c496 to your computer and use it in GitHub Desktop.
Ninja Forms - Pre-populate radio list
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', function($options,$settings){ | |
if( $settings['key'] == 'example_field' ){ | |
// Note! The global $post variable is null when this filter is run | |
// in the context of the merge tag {fields_table}. | |
global $post; | |
$args = array( | |
'post_type' => 'page', | |
'p' => $post->ID, // or get_the_ID() | |
'posts_per_page' => 1 | |
); | |
$the_query = new WP_Query( $args ); | |
if ( $the_query->have_posts() ){ | |
while ( $the_query->have_posts() ){ | |
$the_query->the_post(); | |
$options[] = array( | |
'label' => get_the_title(), | |
'value' => get_the_ID() | |
); | |
} | |
wp_reset_postdata(); | |
} | |
} | |
return $options; | |
},10,2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment