Customizing Auto Populated Field Options In Caldera Forms

Caldera Forms -- Responsive, drag and drop form builder.

In Caldera Forms select fields — dropdown, checkbox and radio — can be automatically populated with all posts of a specific post type or all terms of a specific category. To set this up, you simply need to check the “Auto Populate” option and select whether to use a post type or a taxonomy, and which one.

As of Caldera Forms 1.4.3, you can also use the result of an Easy Queries or Easy Pods query for auto-populating select fields. This provides a simpler — dare I say “easy” — option for using complex queries to customize your select field with out writing a line of code.

The value for the auto-populated options can either be the name or the ID of the post or term. In Caldera Forms 1.2.2 new filters were added to allow you to customize which fields of the post or term are used for the value or label. When auto populating from a post type, you can use any post or meta field associated with the post, thanks to these new filters.

For example, if you are auto-populating with a post type that represents products, and these products have a price set in the meta field “price” you can make your option’s value be the price with this hook and callback:

<?php
add_filter( 'caldera_forms_autopopulate_options_post_value_field', function( $value_field, $field ) {
    if( 'products' == $field[ 'slug' ] ) {
        $value_field = 'price';
    }

    return $value_field;
}, 24, 2 );

For fields populated with taxonomy terms you can use the <code<caldera_forms_autopopulate_options_taxonomy_value_field filter to modify the value. At this time, are limited to the built-in taxonomy fields. For example, to use term slug as the value:

<?php
add_filter( 'caldera_forms_autopopulate_options_taxonomy_value_field', function( $value_field, $field ) {
    if( 'lightsaber_type' == $field[ 'slug' ] ) {
        $value_field = 'slug';
    }
 
    return $value_field;
}, 24, 2 );

What About Customizing The Auto-Populated Options?

Automatically populated field options show all post in a post type or all terms in a taxonomy. There is no way to customize the queries used to get posts or terms. The reason why there is no filter for this specific task is that when such a requirement exists, the auto-population option is not the right tool for the job. Instead use “caldera_forms_render_get_field” filter to customize the options of a select field.

In this example, options for a specific field are set using a posts in the post type “store.” In addition, the query is limited to only stores in a specific state using the meta_key “state”:

<?php
add_filter( 'caldera_forms_render_get_field', function( $field )  {
    if ( 'stores_in_pa' == $field[ 'slug' ]  ) {
        $stores = get_posts( array( 'post_type' => 'stores', 'meta_key' => 'state', 'meta_value' => 'PA' ) );
        if ( ! empty( $stores ) ) {
            foreach( $stores as $store ) {
                $field[ 'config' ][ 'option' ][ $store->ID ] = array(
                    'value' => $store->ID,
                    'label' => $store->post_title
                );
            }
        }
    }

    return $field;

});

Where Does This Code Go?

When using WordPress hooks to customize Caldera Forms or other plugins you should not modify the plugin files, or you will lose your changes when you update the plugin. Instead you should create a small plugin to hold the custom code. It's easy, learn how here.

Technically you can add the custom code to your theme's functions.php, but then you will not be able to change your theme and keep these customizations.

Learn More