Formidable: Populate select field with a post type

Sarah Berg

Setting up options in formidable is easy if you know what your options are.  There are times tho that the list is dynamic and we want to keep it up to date as the site updates. For example, I have a careers post type of available job offerings and I have an application form that they are able to apply for them.  I wanted a select field for the user to find the position they want to apply for and have it hooked up to the list of careers.

Here’s a little something to add into your functions.php file.  Things to know would be:

  • ID of the field set up in formidable
    in this example it is “73”
  • Post Type to query
    this is an example to create a select field filled with job positions available from my “Career” post type.
add_filter('frm_setup_new_fields_vars', 'add_a_field_function', 20, 2);
add_filter('frm_setup_edit_fields_vars', 'add_a_field_function', 20, 2);
 
function add_a_field_function($values, $field){
 
	if($field->id == 73){
		$arrayPositions = array();
		$pushobj = array(
			value => "",
			label => "Select a position..."
		);
		array_push($arrayPositions, $pushobj);
		$positionQuery = new WP_Query( array( 'post_type' => 'career', 'posts_per_page' => -1, 'orderby' => "menu_order", 'order' => "ASC") );
		if ( $positionQuery->have_posts() ) {
			while ( $positionQuery->have_posts() ) {
				$positionQuery->the_post();
				$pushobj = array(
					value => get_the_title(),
					label => get_the_title()
				);
				array_push($arrayPositions, $pushobj);
			}
			wp_reset_postdata();
		}
 
	   	$values['options'] = $arrayPositions;
	}
	return $values;
}

Last updated by on .

What Are Your Thoughts?

Your email address will not be published. Required fields are marked *