Advertisement
rd55

Dynamically Populating Drop Down Fields

Jun 12th, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1. // update the '51' to the ID of your form.
  2. add_filter('gform_pre_render_6', 'populate_posts');
  3. function populate_posts($form){
  4.    
  5.     foreach($form['fields'] as &$field){
  6.        
  7.         if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-posts') === false)
  8.             continue;
  9.        
  10.         // you can add additional parameters here to alter the posts that are retreieved
  11.         // more info: http://codex.wordpress.org/Template_Tags/get_posts
  12.         $posts = get_posts('numberposts=-1&category=1&orderby=title&order=ASC&post_status=publish');
  13.        
  14.         // update 'Select a Post' to whatever you'd like the instructive option to be
  15.         $choices = array(array('text' => 'Select a Post', 'value' => ' '));
  16.        
  17.         foreach($posts as $post){
  18.             $choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
  19.         }
  20.        
  21.         $field['choices'] = $choices;
  22.        
  23.     }
  24.    
  25.     return $form;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement