Skip to content

Instantly share code, notes, and snippets.

@markoheijnen
Created June 20, 2012 07:02
Show Gist options
  • Select an option

  • Save markoheijnen/2958522 to your computer and use it in GitHub Desktop.

Select an option

Save markoheijnen/2958522 to your computer and use it in GitHub Desktop.
Only show users own media files (little bit hackery)
<?php
add_action( 'pre_get_posts', 'media_filter_user' );
add_filter( 'query', 'media_filter_counter' );
// Main stuff that filters it
function media_filter_user( $query ) {
global $pagenow;
if( 'media-upload.php' == $pagenow ) {
$query->set( 'author', get_current_user_id() );
}
}
// Fix queries so the counter is right of all the media types
function media_filter_counter( $query ) {
global $pagenow, $mime_type, $wpdb;
if( ! is_super_admin() && 'media-upload.php' == $pagenow ) {
$traces = debug_backtrace();
if( isset( $traces[ 5 ] ) && 'wp_count_attachments' == $traces[ 5 ]['function'] ) {
if( strstr( $query, 'SELECT post_mime_type' ) ) {
$and = wp_post_mime_type_where('');
return "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_author = " . get_current_user_id() . " $and GROUP BY post_mime_type";
}
else {
$and = wp_post_mime_type_where('');
return "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and";
}
}
else if( 'get_available_post_mime_types' == $traces[ 5 ]['function'] ) {
$query = "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = 'attachment' AND post_author = " . get_current_user_id();
}
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment