PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Populate Dropdown with Parent Taxonomy Only.

  1. bowlsy
    Member

    Have a situation where users will need to select A Goods Sub-Type from a Parent Goods Type. Whilst I can use this plugin to display the hierarchy in one dropdown: http://wordpress.org/extend/plugins/gravity-forms-custom-post-types/ The list of goods sub types listed under goods types is very long and really should be displayed with 2 cascading dropdowns.
    Trying to implement a work around by creating a dropdown that filters just the parent taxonomy terms. Then when a user has selected a parent, one of several hidden child dropdowns will display allowing selection of the goods subtype.

    Managed to get the following code to return the Child Values of a Taxonomy Term in a dropdown:-

    //Adds a filter to form id 1. Replace 1 with your actual form id - CHILD TERMS FOR TRUCKS
    add_filter( 'gform_pre_render_1', 'populate_dropdown' );
    function populate_dropdown($form){
    
        //Grab all Terms Associated with a Specific Taxonomy;
        global $post;
          	$termID = 4;
    	$taxonomyName = "goodscategories";
    	$termchildren = get_term_children( $termID, $taxonomyName );
        //Creating drop down item array.
        $items = array();
    
        //Adding initial blank value.
        $items[] = array("text" => "", "value" => "");
    
        //Adding term names to the items array
        //foreach($terms as $term){
        foreach ($termchildren as $child) {
        $term = get_term_by( 'id', $child, $taxonomyName );
            $is_selected = $term ->name == "testing" ? true : false;
            $items[] = array("value" => $term ->name, "text" => $term ->name, "isSelected"=> $is_selected);
        }
    
        //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["type"] = "select";
                $field["choices"] = $items;
            }
    
        return $form;
    }

    Tried this code for parent dropdown, but returns no values in the dropdown:-

    /Adds a filter to form id 1. Replace 1 with your actual form id - PARENT TERMS ONLY
    add_filter( 'gform_pre_render_1', 'populate_dropdown_parents' );
    function populate_dropdown_parents($form){
    
        //Grab all Terms Associated with a Specific Taxonomy;
        global $post;
           $terms = get_terms("goodscategories", 'parent=0');
    
        //Creating drop down item array.
        $items = array();
    
        //Adding initial blank value.
        $items[] = array("text" => "", "value" => "");
    
        //Adding term names to the items array
        foreach($terms as $term){
            $is_selected = $term->name == "testing" ? true : false;
            $items[] = array("value" => $term->name, "text" => $term->name, "isSelected"=> $is_selected);
        }
    
        //Adding items to field id 10. Replace 10 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"] == 10){
                $field["type"] = "select";
                $field["choices"] = $items;
            }
    
        return $form;
    }

    Any help would be greatly welcome.

    Posted 13 years ago on Monday October 17, 2011 | Permalink
  2. Have a client who needs this exact fuctionality with taxonomies.

    Were you ever actually able to get your code to work? :)

    Posted 12 years ago on Friday December 9, 2011 | Permalink
  3. acsnaterse
    Member

    Is is possible that the "isSelected" property doesn't work anymore? I have exactly the same code, it generates my list perfectly, but it doesn't preselect the right option anymore. However when I "dump" the $items array, I see that the value is in there.

    Any ideas?

    Posted 12 years ago on Tuesday February 21, 2012 | Permalink
  4. Trying to do the same thing as Bowlsy

    Posted 12 years ago on Tuesday April 24, 2012 | Permalink
  5. bradvin
    Member

    This problem is now solved with V4 of the GF+CPT plugin. I have a post outlining these features here : http://themergency.com/gravity-forms-custom-post-types-v4-beta-testers-wanted/

    It is still in beta and I need some testers to test it out, so please lend a hand

    thanks
    Brad

    Posted 12 years ago on Thursday April 26, 2012 | Permalink