Created
June 6, 2017 16:17
-
-
Save sutandang/096eed3c5d7d9198bfdca5a5afd27f71 to your computer and use it in GitHub Desktop.
Filter Wordpress media by user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('pre_get_posts', 'um_filter_media_files'); | |
add_filter('wp_count_attachments', 'um_recount_attachments'); | |
function um_recount_attachments($counts_in){ | |
global $wpdb; | |
global $current_user; | |
$and = wp_post_mime_type_where(''); // Default mime type // AND post_author = {$current_user->ID} | |
$count = $wpdb->get_results("SELECT post_mime_type, COUNT(*) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_author = {$current_user->ID} $and GROUP BY post_mime_type", ARRAY_A); | |
$counts = array(); | |
foreach((array)$count as $row){ | |
$counts[$row['post_mime_type']] = $row['num_posts']; | |
} | |
$counts['trash'] = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_author = {$current_user->ID} AND post_status = 'trash' $and"); | |
return $counts; | |
} | |
function um_filter_media_files($wp_query){ | |
global $current_user; | |
$wp_query = $wp_query; | |
// Check so the $wp_query->query['post_type'] isset | |
if(isset($wp_query->query['post_type'])){ | |
if(get_option('wpusmeadminself') == '1'){ | |
if(current_user_can('manage_options') && (is_admin() && $wp_query->query['post_type'] === 'attachment')){ | |
$wp_query->set('author', $current_user->ID); | |
} | |
} | |
if(!current_user_can('manage_options') && (is_admin() && $wp_query->query['post_type'] === 'attachment')){ | |
$wp_query->set('author', $current_user->ID); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment