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.

Dynamically populating a drop-down custom field

  1. I've snagged some code from this forum that helps me dynamically populate a category of posts into a drop down menu.

    However, If i try to use a custom field drop down menu instead of a regular drop down menu (using same specifications), the drop down will not dynamically populate.

    /* Dynamic drop-down on /register
    ------------------------------------------------------ */
    add_filter('gform_pre_render_1', 'populate_posts');
    function populate_posts($form){
        foreach($form['fields'] as &$field){
            if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-posts') === false)
                continue;
            $posts = get_posts('numberposts=-1&post_status=publish&category_name=members');
            $choices = array(array('text' => 'Select a member company', 'value' => ' '));
            foreach($posts as $post){
                $choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
            }
            $field['choices'] = $choices;
        }
        return $form;
    }

    Any ideas on what I can do differently with this script to ensure I can dynamically populate a custom field (drop down) ?

    Posted 11 years ago on Wednesday August 1, 2012 | Permalink
  2. Do you have a link to this online? And is that the actual code you are using to populate the drop down currently, the non-working code?

    Posted 11 years ago on Wednesday August 1, 2012 | Permalink
  3. Hi, I'm having the same problem. I can get a typical drop-down field to populate with a custom post type, but not the custom-field drop down - it just won't populate. Please help, as this is how Wordpress can really be used like a relational database, by populating custom fields with ID's of custom post types.

    Posted 11 years ago on Thursday August 2, 2012 | Permalink
  4. The issue is that custom fields are a little different than a regular drop down field, and the code needs to be slightly adjusted.
    Try the following:

    add_filter('gform_pre_render_1', 'populate_posts');
    function populate_posts($form){
        foreach($form['fields'] as &$field){
            if($field['inputType'] != 'select' || strpos($field['cssClass'], 'populate-posts') === false)
                continue;
            $posts = get_posts('numberposts=-1&post_status=publish&category_name=members');
            $choices = array(array('text' => 'Select a member company', 'value' => ' '));
            foreach($posts as $post){
                $choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
            }
            $field['choices'] = $choices;
        }
        return $form;
    }
    Posted 11 years ago on Friday August 3, 2012 | Permalink
  5. That worked! Great. Thank you.

    Posted 11 years ago on Friday August 3, 2012 | Permalink
  6. @seahoss, thanks for the update.

    Posted 11 years ago on Saturday August 4, 2012 | Permalink

This topic has been resolved and has been closed to new replies.