This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Show only posts and media related to logged in author

By: a guest on Oct 13th, 2012  |  syntax: None  |  size: 5.43 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  // Show only posts and media related to logged in author
  2.    add_action('pre_get_posts', 'query_set_only_author' );
  3.    function query_set_only_author( $wp_query ) {
  4.      global $current_user;
  5.      if( is_admin() && !current_user_can('edit_others_posts') ) {
  6.         $wp_query->set( 'author', $current_user->ID );
  7.         add_filter('views_edit-post', 'fix_post_counts');
  8.         add_filter('views_upload', 'fix_media_counts');
  9.      }
  10.    }
  11. // Fix post counts
  12.    function fix_post_counts($views) {
  13.     global $current_user, $wp_query;
  14.     unset($views['mine']);
  15.     $types = array(
  16.         array( 'status' =>  NULL ),
  17.         array( 'status' => 'publish' ),
  18.         array( 'status' => 'draft' ),
  19.         array( 'status' => 'pending' ),
  20.         array( 'status' => 'trash' )
  21.     );
  22.     foreach( $types as $type ) {
  23.         $query = array(
  24.             'author'      => $current_user->ID,
  25.             'post_type'   => 'post',
  26.             'post_status' => $type['status']
  27.         );
  28.         $result = new WP_Query($query);
  29.         if( $type['status'] == NULL ):
  30.             $class = ($wp_query->query_vars['post_status'] == NULL)  ? ' class="current"' : '';
  31.             $views['all'] = sprintf(__('<a href="%s"'. $class  .'>All <span class="count">(%d)</span></a>', 'all'),
  32.                 admin_url('edit.php?post_type=post'),
  33.                 $result->found_posts);
  34.         elseif( $type['status'] == 'publish' ):
  35.             $class = ($wp_query->query_vars['post_status'] == 'publish') ? ' class="current"' : '';
  36.             $views['publish'] = sprintf(__('<a href="%s"'. $class .'>Publish <span class="count">(%d)</span></a>', 'publish'),
  37.                 admin_url('edit.php? post_status=publish&post_type=post'),
  38.                 $result->found_posts);
  39.         elseif( $type['status'] == 'draft' ):
  40.             $class = ($wp_query->query_vars['post_status'] == 'draft') ? ' class="current"' : '';
  41.             $views['draft'] = sprintf(__('<a href="%s"'. $class .'>Draft'. ((sizeof($result->posts) > 1) ? "s" : "") .' <span class="count">(%d)</span></a>', 'draft'),
  42.                 admin_url('edit.php?post_status=draft&post_type=post'),
  43.                 $result->found_posts);
  44.         elseif( $type['status'] == 'pending' ):
  45.             $class = ($wp_query->query_vars['post_status'] == 'pending') ? ' class="current"' : '';
  46.             $views['pending'] = sprintf(__('<a href="%s"'. $class .'>Pending <span class="count">(%d)</span></a>', 'pending'),
  47.                 admin_url('edit.php?post_status=pending&post_type=post'),
  48.                 $result->found_posts);
  49.         elseif( $type['status'] == 'trash' ):
  50.             $class = ($wp_query->query_vars['post_status'] == 'trash') ? ' class="current"' : '';
  51.             $views['trash'] = sprintf(__('<a href="%s"'. $class .'>Trash <span class="count">(%d)</span></a>', 'trash'),
  52.                 admin_url('edit.php?post_status=trash&post_type=post'),
  53.                 $result->found_posts);
  54.         endif;
  55.       }
  56.       return $views;
  57.     }
  58.  
  59.    // Fix media counts
  60.    function fix_media_counts($views) {
  61.       global $wpdb, $current_user, $post_mime_types,  $avail_post_mime_types;
  62.       $views = array();
  63.       $count = $wpdb->get_results( "
  64.          SELECT post_mime_type, COUNT( * ) AS num_posts
  65.          FROM $wpdb->posts
  66.          WHERE post_type = 'attachment'
  67.          AND post_author = $current_user->ID
  68.          AND post_status != 'trash'
  69.          GROUP BY post_mime_type
  70.       ", ARRAY_A );
  71.       foreach( $count as $row )
  72.                                 if ($count && $row != 0) {
  73.         $_num_posts[$row['post_mime_type']] = $row['num_posts'];
  74.                 $_total_posts = array_sum($_num_posts);
  75.                 $detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
  76.                                 };
  77.       if ( !isset( $total_orphans ) )
  78.          $total_orphans = $wpdb->get_var("
  79.             SELECT COUNT( * )
  80.             FROM $wpdb->posts
  81.             WHERE post_type = 'attachment'
  82.             AND post_author = $current_user->ID
  83.             AND post_status != 'trash'
  84.             AND post_parent < 1
  85.          ");
  86.                 if ($matches != 0) {
  87.                 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  88.     foreach ( $matches as $type => $reals )
  89.         foreach ( $reals as $real )
  90.             $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
  91.     $class = ( empty($_GET['post_mime_type']) && !$detached && !isset($_GET['status']) ) ? ' class="current"' : '';
  92.     $views['all'] = "<a href='upload.php'$class>" . sprintf( __('Todas <span class="count">(%s)</span>', 'uploaded files' ), number_format_i18n( $_total_posts )) . '</a>';
  93.     foreach ( $post_mime_types as $mime_type => $label ) {
  94.         $class = '';
  95.         if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  96.             continue;
  97.         if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  98.             $class = ' class="current"';
  99.         if ( !empty( $num_posts[$mime_type] ) )
  100.             $views[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), $num_posts[$mime_type] ) . '</a>';
  101.     }
  102.       $views['detached'] = '<a href="upload.php?detached=1"' . ( $detached ? ' class="current"' : '' ) . '>' . sprintf( __( 'Sin Adjuntar <span class="count">(%s)</span>', 'detached files' ), $total_orphans ) . '</a>';
  103.     return $views;
  104.     }
  105.    };
clone this paste RAW Paste Data