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.

Drop Down of Posts in Category?

  1. I'd like to make a form for our users to pick things like "favorite podcast from 2010". All podcasts are in the "podcast" category so my question is:

    How can I populate a drop-down with a list of all Post Titles from a specified Category in a specified date range?

    I haven't created the form yet, but this will go up at http://www.causticsodapodcast.com/ when ready.

    Posted 13 years ago on Thursday January 6, 2011 | Permalink
  2. You can only do this with custom code using available API hooks. You would add custom code to your themes functions.php. Below is a code snippet that is an example. It populates a drop down with a list of posts from a pre-defined category. You would have to customize this code to suit your needs.

    Please note the code snippet below has 3 areas where you need to customize it with your form id, the category you want and the field id of the field you want to pre-populate dynamically.

    <?php
    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 5
    if($form["id"] != 5)
    return $form;
    
    //Reading posts for "Business" category;
    $posts = get_posts("category=Business");
    
    //Creating drop down item array.
    $items = array();
    
    //Adding initial blank value.
    $items[] = array("text" => "", "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 8. Replace 8 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"] == 8){
    $field["choices"] = $items;
    }
    
    return $form;
    }
    ?>
    Posted 13 years ago on Thursday January 6, 2011 | Permalink
  3. Amazing quick response. Thanks!

    Posted 13 years ago on Thursday January 6, 2011 | Permalink
  4. Just wanted to note two things. Firstly, this is up and active on my podcast's site:
    http://www.causticsodapodcast.com/season-one-poll/

    Secondly, I had to add this before line 38 above (just above return$form;) to clear the query. When it wasn't there the page showed every one of the posts it had queried below the form!

    wp_reset_query();

    Posted 13 years ago on Monday January 31, 2011 | Permalink
  5. Hi Carl,

    I tried doing this, but it only works on the live form.
    For some reason I can't see the choices on the admin page (I still see "First Option, Second Option, Third Option")

    Any idea what I'm doing wrong?

    Here is my code hook:

    add_filter("gform_pre_render", "populate_dropdown");
    
    add_filter("gform_admin_pre_render", "populate_dropdown");
    
    function populate_dropdown($form) {
    
    	$vectors = array();
    	$vectors[0] = array("value" => "pJ201", "text" => "pJ201");
    	$vectors[1] = array("value" => "pJ204", "text" => "pJ204");
    	$vectors[2] = array("value" => "pJ206", "text" => "pJ206");
    	$vectors[3] = array("value" => "pJ241", "text" => "pJ241");
    	$vectors[4] = array("value" => "pJ244", "text" => "pJ244");
    	$vectors[5] = array("value" => "pJ246", "text" => "pJ246");
    
    	foreach($form["fields"] as &$field) {
            	if($field["adminLabel"] == "Vector"){
               		$field["choices"] = $vectors;
    		}
            }
    	return $form;
    }
    Posted 12 years ago on Monday May 23, 2011 | Permalink
  6. You won't see it in the admin. That is the pre render hook which means it only executes before a form is displayed on the frontend of your site. You aren't going to see it reflected in the admin.

    Posted 12 years ago on Monday May 23, 2011 | Permalink
  7. So... I took the above code from the example at: http://www.gravityhelp.com/documentation/page/Gform_pre_render

    You'll notice I'm hooking into both "gform_pre_render" and "gform_admin_pre_render".

    What does "gform_admin_pre_render" do then?
    Is there a way to populate the list in the admin side?

    Thanks,

    Alan

    Posted 12 years ago on Monday May 23, 2011 | Permalink
  8. gform_admin_pre_render is used on the Entry Detail page to manipulate what happens when you edit an Entry. It isn't used in the Form Editor itself.

    Posted 12 years ago on Monday May 23, 2011 | Permalink
  9. OK.

    I am trying to find a way to let the admin users create conditional logic based on values that would come from our database. Perhaps I am attacking this the wrong way.

    Is there a way to populate the list in the admin side? Or is there another form of functionality that would allow me do this?

    Posted 12 years ago on Monday May 23, 2011 | Permalink
  10. Conditional Logic does not currently work with dynamically populated drop downs. Conditional Logic only works with the static values. This isn't currently possible and i'd have to check with our lead developer to find out what could be done to support this. The problem is in how the conditional drop down UI works, your custom code isn't going to be able to manipulate the options.

    Posted 12 years ago on Monday May 23, 2011 | Permalink
  11. I see.

    Is there a workaround you could suggest?

    Is there a way to populate something like a hidden field with code, and then drive the conditional logic off that?

    Or is there someway to show / hide fields dynamically with code hooks?

    Thanks again,

    Alan

    Posted 12 years ago on Monday May 23, 2011 | Permalink
  12. As I mentioned, i'll have to discuss this with our lead developer. It's my understanding that there is not, but we can look at it and see if there are any additional hooks we can add to make it so you can dynamically populate drop downs/checkbox/radio buttons AND have it reflected in conditional logic options. He is currently traveling so I won't be able to discuss this with him until tomorrow.

    Posted 12 years ago on Monday May 23, 2011 | Permalink
  13. creativitymill
    Member

    Is it possible to populate a drop-down using a wp_nav_menu? I'm building a site for a restaurant with 22 different locations, and one of the forms is a newsletter signup. I've got the locations list populating in various parts of the site from a WP Menu, and was wondering if I can use that to generate the drop-down, so they don't have to edit the list in multiple locations if there are changes.

    Thanks!

    Posted 12 years ago on Saturday July 16, 2011 | Permalink
  14. You can dynamically populate a drop down field with anything you want, you just need to know how to write the code to return the values you want... in this case you'd need to know how to query and return the items in the WP Menu.

    Here is a tutorial on how to dynamically populate a drop down:

    http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields

    Posted 12 years ago on Monday July 18, 2011 | Permalink
  15. Hey!

    What's the situation with dynamically populating drop downs AND combining it with conditional logic options?

    I would like to create a site where people:
    Choose country (countries ready)
    This opens new drop down 'University in Spain' (i.e. if country=Spain)
    Then there would be a text field where you could insert new university if it's not in the drop down list. And this entry would be in the drop down list (University in Spain) for next user who uses the form.

    So is there any way to do this?

    Form: http://www.vaihtoon.fi/WordPress/share

    Posted 12 years ago on Tuesday December 6, 2011 | Permalink
  16. The situation is the built in conditional logic only works off of static values and doesn't currently take into account being triggered by dynamically populated fields. This is a feature we'd like to tackle in the future but there have been higher priority features we have been working on.

    It's one of many features we'd like to add and will eventually be added as we are constantly working in improving and enhancing Gravity Forms.

    The only way to do this now would be as a customization that would be fairly complex because you'd have to implement your own conditional logic as well as dynamically populate drop down fiels. There isn't currently a tutorial on this type of complex customization.

    Posted 12 years ago on Tuesday December 6, 2011 | Permalink
  17. Ok..

    So I'm going to do it without the conditional logic. What would I need to add to get the dynamic population to work for the 'City' drop down. If the City that a form user wants to select is not ready in the drop down list, he would be able to write to the 'City if not in the list' and next user of the form would see that one in the 'City' drop down. I tried to follow the guide, but didn't quite get how I should determine that it takes the text from the 'CIty if not in the list'.

    http://www.vaihtoon.fi/WordPress

    Posted 12 years ago on Thursday December 8, 2011 | Permalink
  18. This is close to what I want to do, but I want to add in extra info to:

    $items[] = array("value" => $post->post_title, "text" => $post->post_title);

    I want the value to be: thePostTitle AND a Category. So the out put would be: Lowdown Brass band (post title) - Friday (taxonomy)

    Do this make sense? I'm trying to make a more descriptive title by pulling in custom taxonomies.

    Posted 11 years ago on Friday July 6, 2012 | Permalink
  19. Hi - I am doing something wrong! I, too, want just a category to show up in my drop down. The category name is Now Booking. The slug is now-booking. the cat id is 61.

    Here is my functions code that is currently pulling up ALL posts, not the specific category I am trying to get:

    <?php
    
    // update the '51' to the ID of your form
    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;
    
            // 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('category=now-booking&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 11 years ago on Thursday August 2, 2012 | Permalink
  20. David Peralty

    Try changing it to category=61 and numberposts to a real number... say 10 or so and see if that works.

    Posted 11 years ago on Thursday August 2, 2012 | Permalink