I've searched numerous StackOverflow and Wordpress Awnsers questions, but I couldn't find any solution that matches my situation or even remotely points me in the right direction.
I have a custom media frame in my plugin where I have pre-selected certain images out of a media library of about 3000 images. However, these pre-selected images aren't sorted on top of the list by default, which results in you browsing the list for hours to find your selected images. In short I want these to be on top of the list. I have their ID's.
I found that in media-models.js where the code goes:
Query = media.model.Query = Attachments.extend({ ...
it handles the querying / sorting of the attachments that are displayed in the media manager.
I've tried extending it like this in my main plugin javascript file
wp.media.model.Query = wp.media.model.Attachments.extend({ ...
which gave me no result and numerous other suggestions in question that looked similar to my situation. Now I'm at the point where I'm out of idea's of how to tackle this. Could someone point me in the right direction?
EDIT: Well I have been able to influence the queries made by my media frame.
In my main.js in my plugin I do this now:
file_frame = wp.media.frames.file_frame = wp.media({
id: 'multi_image',
title: 'Select images',
button: {
text: 'Use selected images'
},
library: {
type: 'image', post__in:[3322,3323,3324,3325,3326,3327,3328]
},
multiple: true,
});
This way I can show the media frame only displaying the image attachments with certain id's. However I also want to display the rest of the attachments, only with my selected images on top. So I would need to either combine the results of this query by making a new query with post__not_in instead of post__in or by custom ordering the attacments giving the attachments with those id's priority. I don't know how to do that. The arguments I can pass to the library seem limited to me.
$GLOBALS['wpdb']->last_query
and$GLOBALS['wpdb']->last_result
in a callback hooked topre_get_posts
. If you find the query that is responsible, you can simply narrow your scenario down until all other queries are excluded. Update your question in between.