WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

Your Answer


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.