WordPress.org

Ready to get started?Download WordPress

Forums

Gravity Forms + Custom Post Types
Reorder a Drop Down Populated with a Custom Post Type (4 posts)

  1. fettabachi
    Member
    Posted 1 year ago #

    I tried to reorder the drop down fields but after saving my form they revert to the default order which is id# descending.

    How can I control the ordering?

    and

    How can I change the value?

    Thanks

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

  2. mealtime
    Member
    Posted 11 months ago #

    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.

  3. fettabachi
    Member
    Posted 11 months ago #

    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;
    }
  4. mealtime
    Member
    Posted 11 months ago #

    Thanks! Will test it out :-)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic