WordPress.org

Support

Support » Plugins and Hacks » Reorder a Drop Down Populated with a Custom Post Type

Reorder a Drop Down Populated with a Custom Post Type

Viewing 3 replies - 1 through 3 (of 3 total)
  • mealtime
    Member

    @mealtime

    Would also like to find this out as the same problem occurs with taxonomies. If you uncheck populate with a taxonomy or post types, the order will be saved, but once you check it off again, the order diverts back to being alphabetical.

    fettabachi
    Member

    @fettabachi

    I used this in my functions file instead;

    /////////////////////////////////////////////////////////////////////////////////
    // Add Camp Sessions to Gravity Form select list
    
    add_filter("gform_pre_render", "populate_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_dropdown");
    
    function populate_dropdown($form){
    
        //only populating drop down for form id 1
        if($form["id"] != 1)
           return $form;
    
        //Reading posts for the Camp Sessions post type;
            $args = array(
               'posts_per_page' => -1,
               'post_type'      => 'camp_sessions',
               'post_status'    => 'publish',
               'orderby'        => 'title',
               'order'          => 'ASC',
            );
            $posts = get_posts ( $args );
    
        //Creating drop down item array.
        $items = array();
    
        //Adding initial value.
        $items[] = array("text" => "Please select a session", "value" => "");
    
        //Adding post titles to the items array
        foreach($posts as $post)
            $items[] = array("value" => $post->post_title, "text" => $post->post_title);
    
        //Adding items to field id 14. You can get the field id by looking at the field name on the form.
        foreach($form["fields"] as &$field)
            if($field["id"] == 14){
                $field["choices"] = $items;
            }
    
        return $form;
    }

    mealtime
    Member

    @mealtime

    Thanks! Will test it out 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Reorder a Drop Down Populated with a Custom Post Type’ is closed to new replies.