5

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.

3
  • 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, 2012 at 14:44
  • @brasofilo thanks for your comment. I have solved the problem and posted my solution below. Jun 26, 2012 at 11:07
  • Beautiful solution, kudos for that!
    – brasofilo
    Jun 26, 2012 at 11:20

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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