Advertisement
chrishajer

Display custom post type only for logged in user

Oct 7th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php
  2. // Original http://pastie.org/1617402
  3. // Original by David Smith, Ounce of Talent http://ounceoftalent.com/about/
  4. // Modified by Chris Hajer October 7, 2012 for
  5. // http://www.gravityhelp.com/forums/topic/drop-down-with-posts-from-certain-author#post-79310
  6.  
  7. /* Display Posts as Choices */
  8.  
  9. add_action('gform_pre_render', 'populate_post_type');
  10. function populate_post_type($form){
  11.    
  12.     $dropdown_field = 2; // update to the ID of your drop down field
  13.     $post_type = 'review'; // update
  14.    
  15.     $choices = ounce_posts_as_choices($post_type);
  16.    
  17.     foreach($form['fields'] as &$field){
  18.        
  19.         if($field['id'] == $dropdown_field)
  20.             $field['choices'] = $choices;
  21.  
  22.     }
  23.    
  24.     return $form;
  25. }
  26.  
  27. function ounce_posts_as_choices($post_type = 'post', $first_choice = '') {
  28.    
  29.     $posts = get_posts("post_type=$post_type");
  30.    
  31.     // determine the logged in user
  32.     global $current_user;
  33.     get_currentuserinfo();
  34.     $userid = $current_user->ID;
  35.    
  36.     $choices = array();
  37.     $i = 0;
  38.    
  39.     if($first_choice){
  40.         $choices[$i]['text'] = $first_choice;
  41.         $choices[$i]['value'] = '';
  42.         $i++;
  43.     }
  44.    
  45.     foreach($posts as $post) {
  46.         // only return posts for the logged in user
  47.         if ($userid == $post->post_author) {
  48.             $choices[$i]['text'] = $post->post_title;
  49.             $choices[$i]['value'] = $post->ID;
  50.             $i++;
  51.         }
  52.     }
  53.    
  54.     return $choices;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement