Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have made a plugin that uses the Media Library to allow users to upload files to a specific directory - using the upload_dir filter.

I would like to know if there is a way (i.e. a filter) I can use to limit the media library to displaying only files contained within my custom folder?

If possible, I want the user to be able to choose only files that have been uploaded to the custom folder when interacting with the Media Library instantiated by my plugin.

share|improve this question
    
Tricky question... Cannot dig into it, but maybe a combination of pre_get_posts and parse_query with a temporary modification of wp_upload_dir() value could handle it... This Q could also be useful.. –  brasofilo Jun 25 '12 at 14:44
    
@brasofilo thanks for your comment. I have solved the problem and posted my solution below. –  Michael Robinson Jun 26 '12 at 11:07
    
Beautiful solution, kudos for that! –  brasofilo Jun 26 '12 at 11:20
add comment

1 Answer

up vote 2 down vote accepted

A solution that works for me is to add a clause to the Wordpress query when the media library is being displayed.

From browsing my Wordpress database I noticed that the full path to wp_posts.post_type = 'attachment' is stored in the wp_posts.guid column.

add_filter('posts_where', 'limitMediaLibraryItems_56456', 10, 2 );
function limitMediaLibraryItems_56456($where, &$wp_query) {
    global $pagenow, $wpdb;

    // Do not modify $where for non-media library requests
    if ($pagenow !== 'media-upload.php') {
        return $where;
    }

    $where .= " AND {$wpdb->posts}.guid LIKE '%my-path-segment%'";

    return $where;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.