How to Filter in the "Add Media Popup" to show only "unattached" Media

Any idea how to hack WP 3.8.x for this feature?

Topic media-library wp-admin Wordpress

Category Web


Can't deliver a working example but there is this relevant section in wp-includes/js/media-views.js on Line 3173:

media.view.AttachmentFilters.Uploaded = media.view.AttachmentFilters.extend({
    createFilters: function() {
        var type = this.model.get('type'),
            types = media.view.settings.mimeTypes,
            text;

        if ( types && type )
            text = types[ type ];

        this.filters = {
            all: {
                text:  text || l10n.allMediaItems,
                props: {
                    uploadedTo: null,
                    orderby: 'date',
                    order:   'DESC'
                },
                priority: 10
            },

            uploaded: {
                text:  l10n.uploadedToThisPost,
                props: {
                    uploadedTo: media.view.settings.post.id,
                    orderby: 'menuOrder',
                    order:   'ASC'
                },
                priority: 20
            }
        };
    }
});

One filter refers to uploadedTo: null and the other one to uploadedTo: media.view.settings.post.id. So this is equivalent to "All Attachments" and "attached to the current post".

When an attachment is unattached its uploadedTo value is 0. I just tried to create a new filter for that but it did not return any attachments at all (I do have unattached uploads in my db).

Line 581 of wp-includes/js/media-models.js shows how the filtering mechanism is constructed:

uploadedTo: function( attachment ) {
    var uploadedTo = this.props.get('uploadedTo');
        if ( _.isUndefined( uploadedTo ) )
            return true;

        return uploadedTo === attachment.get('uploadedTo');
    }

I had guessed that a zero value would pass _.isUndefined() but it does not. So I'm pretty much clueless here. But maybe this pushes you or somebody else into the direction. The media framework is still mostly undocumented.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.