I have wordpress web site which contains over 150 000 images and when you click on "Set Featured Image" whole server slows down. Another thing is that it take a really long time to load images.

Is there any filter/action/hook to add pager or to just show last 10 images.

Another question is how to optimize whole thing (maybe images sort by subfolders with some plugin) so it can work

  • 2
    There's only so much you could do to optimize the system.. Posts, pages, revisions and attachments all share the same database table which means that it's probably huge. Have you upgraded your hardware along with your site growth? If you query posts, pages or attachements, it will need to load the whole table to server's memory and if it doesn't fit to memory, it's A LOT slower.
    – N00b
    Commented May 11, 2016 at 7:19
  • 1
    What are the slow SQL queries here? Is it only the "Set Featured Image" media view that's slow and not the "Add Media" view? It should be paginated via ajax when you scroll down. The month-year query can be slow on large sites and also the video/audio queries. See e.g. #31071
    – birgire
    Commented May 11, 2016 at 9:03

1 Answer 1


Update 1:

After dived into core AJAX call, this filter will only happen on post.php page:

add_filter('ajax_query_attachments_args', function($query){
    if ( isset($_POST['post_id']) && !empty($_POST['post_id']) ) {
        $query['posts_per_page'] = 10; // output 10 images only.
    return $query;

  1. You can use ajax_query_attachments_args filter:
add_filter('ajax_query_attachments_args', function($query){
    $query['posts_per_page'] = 10; // output 10 images only.
    return $query;
  1. Because of querying attachments happens in core and using AJAX, I don't think we can optimize the whole thing.
  • This was my first thought, but unfortunately setting posts_per_page to anything different from -1 will break the infinite scroll. Another point is that this has a global scope, affecting not just the featured image in post types but the Media Library manager.
    – Luis Sanz
    Commented May 11, 2016 at 11:08

Your Answer

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

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