Gravity Form Field to Custom Post Type

This is a minor tweak if you’re using Gravity Forms with the Advanced Post Creation Plugin. Not much of a post but it never hurts to have more documentation out there.

If you’re not using the Advanced Post Creation plugin, then this snippet works perfectly. If you are using it, the dropdown will be right but you’ll end up with new categories created that are the term ID. You don’t want that.

It’s just a matter of the APC plugin trying to help too much. The fix is easy. Just set the value to be $term->name (rather than $term->term_id).

The example below applies to a form with ID 1 and ties the custom field ‘years’ to the field with ID 7.

add_filter('gform_pre_render_1', 'cftg_populate_fellow_year');
function cftg_populate_fellow_year($form){


    $terms = get_terms( array(
        'taxonomy' => 'years',
        'hide_empty' => false,
        'orderby'   =>'title',
        'order'   =>'ASC',
    ) );

    //Creating drop down item array.
    $items = array();

    //Adding initial blank value.
    $items[] = array("text" => "", "value" => "");

    //Adding post titles to the items array
    foreach($terms as $term)
        $items[] = array(
           "value" => $term->name, //*************SET THIS TO BE THE NAME *************
           "text" =>  $term->name
      );

    //Adding items to field id 7
    foreach($form["fields"] as &$field)
        if($field["id"] == 7){
            $field["type"] = "select";
            $field["choices"] = $items;
        }

    return $form;
}