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.

41 thoughts on “Search through all fields in Media Library”

  1. 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.
    Thanks!

    Reply
  2. 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?

    Reply
    • 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.

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

      Reply
  3. 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?

    Reply
  4. 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.

    Reply
    • 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.

      Reply
  5. 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?

    Reply
    • 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.

      Reply
  6. 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] 1fix.io. Thanks!

    Reply
  7. Hi there,
    Not sure if this is a related question or not, but I’ve added keywords to my local image library and am trying to figure out how to get those keywords to auto populate the alt field when I upload the images to the media folder. I use Photoshop to edit the file info, which is where I add those keywords. So far, those keywords don’t appear on upload though. Any idea where I *should* be adding them?

    Thanks in advance.

    Reply
  8. I still use this plugin and still love it! But when I recommend it to friends, what they see in the WordPress plugin repository is that it hasn’t been updated for three years and hasn’t been tested beyond WordPress 4.4.16. Are there any plans to ensure its compatibility with WordPress 5.0?

    Reply
  9. Hey Ken!

    Interesting – I didn’t think people would still use it honestly. I’ll do some tests and see if it can work with 5.0. Thanks for nagging me about it. 🙂

    Reply
    • Hey Michel,

      Thanks for letting me know. I haven’t gotten around to do any tests but if you notice anything that breaks it and WP 5.1, please let me know.

      Reply
      • Hi Yoren!

        It has now come to this. With the latest update of wordpress 5.5.1, the media search enhanced plugin does not work properly. I also use WP media manager lite plugin and the directories are no longer searched. The photos that I know are there are no longer found. I really hope it can be resolved soon.

        Reply
          • hi Michel,

            I have tested it in 5.5.1 right in the Media Library and it works for me. Can you disable that plugin you mentioned and try again? (I couldn’t find the plugin you mentioned).

            If it doesn’t work for you in the Media Library, can you describe the steps of your search so I can try to reproduce it? Thanks

          • Hi Yoren!

            I deactivated the plugin I’m working with (https://wordpress.org/plugins/wp-media-manager-lite/), and that made your plugin work again. Unfortunately, I really need that manager plugin, just like the media search enhanced plugin.

            With both plugins installed, entering a word in the search field will not return any results.

            I hope you can find a solution for it.

            Thanks in advance!

  10. WordFence Security plugin is flagging this plugin as a possible security risk. Could you please update at least the version number, so it will not appear to be an abandoned (and potentially dangerous) plugin? This is a very useful plugin and I use it on several sites.

    Thank you!

    Reply
    • Hi Paul,

      I’m not aware of that. It’s indeed has been updated for a while and if bumping the version can work (I doubt it tho), I would try. Let me check the WP.org repo this weekend and see if I can find something. Thanks!

      Reply
  11. Hi Yoren,

    I was wondering if you have a solution to search for multiple images/files in the WordPress media library. So if I have two different unique texts to search for which match different image files, is there a way to enter those texts in such way that they can be displayed in the media library simulateneously instead of having to create all individual searches…

    I tried different search terms separated by comma (image1, image2) and ampersand etc (image1 & image2) in the the search field, but that clearly doesn’t work…

    Any idea/solution for this ? That would make cleaning out products in a woocommerce store much easier…

    Thank you for your comments!

    Frederic

    Reply

Leave a Comment