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.

Dynamic population doesn't work !

  1. Hello,
    I follow all the steps of this page (http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields). I copied the code in my functions.php file and changed the ID of the form to 6 (the ID of my form).

    I've created a test form with a dropdown field (http://www.piratecafe.eu/test/). The field has the checkbox 'Allow field to be populated dynamically' checked, with parameter name 'posts'.

    The page where the form should be shown has this shortcode: [gravityform id="6" name="Untitled Form" ajax="true"]

    But the result is always the same; the default data of the drop-down gets filled in instead of the post titles I requested. I get the feeling that my website isn't communicating with the function, but it might be something else altogether. I've got no clue of what I'm doing wrong, some help is appreciated. I spent a few hours reading and trying out multiple examples with no positive results...

    Thanks in advance!

    Once again the entire code from functions.php:

    // update the '51' to the ID of your form
    add_filter('gform_pre_render_6', 'populate_posts');
    function populate_posts($form){
    
        foreach($form['fields'] as &$field){
    
            if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-posts') === false)
                continue;
    
            // you can add additional parameters here to alter the posts that are retreieved
            // more info: http://codex.wordpress.org/Template_Tags/get_posts
            $posts = get_posts('numberposts=-1&post_status=publish');
    
            // update 'Select a Post' to whatever you'd like the instructive option to be
            $choices = array(array('text' => 'Select a Post', 'value' => ' '));
    
            foreach($posts as $post){
                $choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
            }
    
            $field['choices'] = $choices;
    
        }
    
        return $form;
    }
    Posted 7 years ago on Tuesday February 12, 2013 | Permalink
  2. The CSS Class name should be "populate-posts" not "posts", according to the example code and the code you have pasted here.

    Posted 7 years ago on Wednesday February 13, 2013 | Permalink
  3. Thks a lot !

    Posted 7 years ago on Thursday February 14, 2013 | Permalink
  4. You're welcome. Is this working for you now?

    Posted 7 years ago on Sunday February 17, 2013 | Permalink