Search through all fields in Media Library

My 3rd plugin – Media Search Enhanced is to solve an issue in a client’s project (again!). In this project, the client uploads tremendous images to the Media Library and later they reported a search bug.

When they searched the Media Library, some images showed up in the search results, but some didn’t. When I took a deeper look into this, I found the keyword search seemed to skip the Caption and Alternative Text fields. If the keyword is in these 2 fields, the image will not be included in the search results.

It’s actually not a bug, the way WordPress does the search leads to it. In a default WordPress search, which means query posts with the variable “s”, it only search for “post_title” and “post_content” in the posts table.

So I tracked down to figure out where all data in the Media fields are saved to. And here is data mapping:

  • Title – post_title in the posts table
  • Caption – post_excerpt in the posts table
  • Alternative Text – meta_value in the postmeta table, the meta_key is “_wp_attachment_image_alt”
  • Description – post_content in the posts table

To include post_excerpt and a certain meta_value in the search fields, the best chance is to use the “post_clauses“, or these clause-specific filters:

  • posts_where
  • posts_join
  • posts_distinct

So in Media Search Enhanced, I use 3 functions to hook to the filters above, and am going to add more features to it soon:

  • TODO – Search media file name.
  • TODO – Add result snippet column which shows text snippets around found keyword on the Media Library screen.
  • TODO – Search Media Categories, Tags or Custom Taxonomies.

One thing I’d like to note is, WP_Query could take lots of parameters to do the posts query, but the “s” can’t be used with “meta_query” at the same time, because when doing so, the WHERE statement in the SQL would be like:

” AND ( ( wp_posts.post_title LIKE ‘%keyword%’) OR ( wp_posts.post_content LIKE ‘%keyword%’) ) AND ( wp_postmeta.meta_key = ‘_wp_attachment_image_alt’ AND wp_postmeta.meta_value LIKE ‘%” . $vars[‘s’] . “%’ )

Which prevent the posts being found for the WHERE statement is much unlikely to sustained.

Here are 2 screenshots to demo the Media Library search results after installing Media Search Enhanced. It just a simple plugin but should fix an issue lots WP users ever experienced.

I’m a senior web developer helping clients build their websites to grow businesses. Currently I’m based in Taipei, Taiwan.

I write things about WordPress, AngularJS and life. Whenever you’d like to find someone to talk about these topics, just get in touch!


Submit a comment
  • Good plugin Yoren.

    Could you let me know your email address? I could not find it on your website.

  • Hi,
    I desperately need to use your plugin on the Front End of my WordPress blog so users can find images. I am not interested in searching the content of the posts. Only the descriptions, captions and alt tags of the images in the image library. The blog is a photo blog for selling images.

    Can you advise me how to modify the code to accomplish that? I would place the search box in a php enabled widget in the sidebar. Results would need to be displayed on the front end “search results” page.

  • I’m currently using this for searching through PDFs on my website, but I just have one problem. I’d like to link to the PDFs directly rather than their attachment page, is there any way to go about making a few changes to this plugin to do just that for PDF files?

    • Hi, Michael, thank you for your feedback. I think I could add a filter for replacing the media link in the next release of Media Search Enhanced. Should be in one week or so. I’ll send you an email then.

  • Hello Yoren,

    This is a very useful plugin, thank you for sharing with the community!

    As we use the image IDs a lot to build ou update galleries on our site, I was wondering if you can consider adding the ID as a criteria for the search, same as this plugin does:

    Many thanks for your answer,

    • Hi, Stefano, I’ll try to integrate the ID field into search in the next release. Will let you know by email then. Thanks.

  • Great plugin, does exactly what it says!

    One question though, I’m trying to use it in conjunction with the file type filters but it seems to ignore/override this.

    any thoughts?

  • Hello
    thank you for your post.
    I got an issue when uploading images that have exif datas.
    WP adds automatically exif datas as media title.
    And it’s a pain to search images by filenames.

    • Hi Simon, Not sure if your issue has to do with my MSE plugin. Guessing you’re just complaining about WP? I haven’t noticed such issue before but I can totally understand that’s annoying.

  • Yoren, I am trying to add your plugin for a search that all can use behind a password protected page. When I type a word that should come up I get nothing? Is there a setting I missed?

    • Hi Dave, you can check if it works in your media library (at WP admin dashboard), if it does, there might be some compatibility issues between your theme and MSE.

    • Hey Joel,

      My best guess is MSE conflict with your theme or plugin since it doesn’t seem to search for media files but your events.

  • I would suggest you to talk with your theme developer if he/she’s available. For freelance inquiry can you please send me an email to yoren [at] Thanks!

Submit a comment

Your email address will not be published. Required fields are marked *