Fill a Fluent Forms Drop-Down With Your Posts
Fills a Fluent Forms drop-down with the titles of your published posts, pages or a custom post type, so the options stay in sync with your content.
Opens WPIDE on
Code
add_filter( 'fluentform/rendering_field_data_select', function ( $data, $form ) {
$class = isset( $data['settings']['container_class'] ) ? (string) $data['settings']['container_class'] : '';
if ( ! preg_match( '/\bwpide-posts-([a-z0-9_-]+)\b/', $class, $m ) || ! post_type_exists( $m[1] ) ) {
return $data;
}
$cache = 'wpide_ff_choices_' . $m[1];
$options = get_transient( $cache );
if ( false === $options ) {
$options = array();
$ids = get_posts( array( 'post_type' => $m[1], 'post_status' => 'publish', 'numberposts' => 200, 'orderby' => 'title', 'order' => 'ASC', 'fields' => 'ids', 'no_found_rows' => true ) );
foreach ( $ids as $id ) {
$title = wp_strip_all_tags( get_the_title( $id ) );
$options[] = array( 'label' => $title, 'value' => $title, 'calc_value' => '' );
}
set_transient( $cache, $options, HOUR_IN_SECONDS );
}
$data['settings']['advanced_options'] = $options;
return $data;
}, 10, 2 );
add_action( 'save_post', function ( $post_id, $post ) {
delete_transient( 'wpide_ff_choices_' . $post->post_type );
}, 10, 2 );
Notes
Add the container class wpide-posts-<post type> (for example wpide-posts-page) to the drop-down in its Advanced Options.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.