WordPress.org

Ready to get started?Download WordPress

Forums

Gravity Forms + Custom Post Types
Dropdown populated with custom post type from within cus (2 posts)

  1. Primitive
    Member
    Posted 1 year ago #

    Hi,

    First of all thanks for the amazing plugin - it has made using Gravity Forms for the site I'm creating a breeze!

    I'm already using the beta of this plugin, but I am stuck trying to work out a problem. I'm trying to create a dropdown populated with all the custom posts that have a certain term in a custom taxonomy.

    I know I can create a drop-downs that are populated with taxonomies which from which I can exclude certain terms, but I'm stuck with this problem...

    Anyone got any ideas on how to do this?

    Thanks!

    http://wordpress.org/extend/plugins/gravity-forms-custom-post-types/

  2. Primitive
    Member
    Posted 1 year ago #

    I couldn't work out how to do the above with this plugin, but it was simple enough to write as a function declared in my functions.php file

    ###### START GRAVITY FORMS SELECT PRODUCER ######
    
    add_filter("gform_pre_render", "populate_producers_dropdown");
    
    //Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
    add_filter("gform_admin_pre_render", "populate_producers_dropdown");
    
    function populate_producers_dropdown($form){
    
    //only populating drop down for form id 3
    if($form["id"] != 3)
    return $form;
    
    //Reading posts for custom post type "place" with custom taxonomy "placecategory" and term "producer";
    
    $args = array(
        'orderby'         => 'title',
        'order'           => 'ASC',
        'post_type'       => 'place',
    	'taxonomy' 	=> 'placecategory',
    	'term' => 'producers'
     );
    
    $posts = get_posts($args);
    
    //Creating drop down item array.
    $items = array();
    
    //Adding initial blank value.
    $items[] = array("text" => "", "value" => "");
    
    //Adding post titles to the items array
    foreach($posts as $post)
    
    $items[] = array("value" => $post->ID, "text" => $post->post_title);
    
    //Adding items to field id 9. Replace 9 with your actual field id. You can get the field id by looking at the input name in the markup.
    foreach($form["fields"] as &$field)
    if($field["id"] == 9){
    $field["choices"] = $items;
    }
    
    return $form;
    }
    
    ###### END GRAVITY FORMS SELECT PRODUCER ######

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic