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 responses

  1. Good plugin Yoren.

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

    1. Yoren Chang Avatar
      Yoren Chang

      Noam, my email is 1fixdotio at gmail.com. Thanks.

  2. 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!

    1. Yoren Chang Avatar
      Yoren Chang

      I’ll do some tests to see how to make it work this way. Talk you later.

      1. Hi,

        Great plugin, did you ever complete this to work on the default front-end search?

        Thanks,
        Tom

  3. […] pushed the latest version of Media Search Enhanced to WP.org, the major updates are inspired by a comment left here last week. Basically Don requested if I can make the plugin work on the frontend, so […]

  4. Michael Murphy Avatar
    Michael Murphy

    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?

    1. Yoren Chang Avatar
      Yoren Chang

      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.

  5. 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: https://wordpress.org/plugins/search-by-id/

    Many thanks for your answer,
    Stefano

    1. Yoren Chang Avatar
      Yoren Chang

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

  6. 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?

    1. Yoren Chang Avatar
      Yoren Chang

      Hey I’ll test it out and release an update if needed. Thanks!

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

    1. Yoren Chang Avatar
      Yoren Chang

      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.

  8. 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?

    1. Yoren Chang Avatar
      Yoren Chang

      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.

  9. Hi, I have installed your plugin and can’t seem to search via file name :

    http://camdensquare.co.uk/myfordphoto/?s=228722&post_type=attachment

    Am i doing something wrong?

    1. Yoren Chang Avatar
      Yoren Chang

      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.

      1. I’m unsure as to what that means, is there any chance you would be able to take a quick look for me? I would really appreciate it. Thank you.

        1. Yoren Chang Avatar
          Yoren Chang

          Hey unfortunately I don’t provide such free support for my free plugins. Thanks for your understanding.

          1. Do you have a day / hour rate to investigate and fix for me? Thanks.

  10. Yoren Chang Avatar
    Yoren Chang

    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!

  11. […] it’s nearly impossible to find an image. Media Search Enhanced is a new plugin created by Yoren Chang that enables all fields for a media item to be searchable. With the plugin activated you […]

  12. 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.

    1. Note: I did find a solution. I can edit the IPTC data for an image within Photoshop. That seems to have fixed things mostly.

      However, I’m also wondering if you’ve figured out a way to make this search work on the front end?

      1. Yoren Chang Avatar
        Yoren Chang

        hi Laura,

        I’m not familiar with how to import image metadata into WP media library, but it looks you’ve figured that part.

        For using a frontend search form, you can check if this can help: https://1fix.io/blog/2015/02/05/frontend-media-search-form/

  13. 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?

  14. Yoren Chang Avatar
    Yoren Chang

    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. 🙂

  15. Hi!

    I think there are many, many people that use this plugin. Mainly because there is not really an alternative. I run a news site and i can’t work without it. So I hope you can bring an update very soon!

    1. Yoren Chang Avatar
      Yoren Chang

      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.

      1. Michel van der Graaf Avatar
        Michel van der Graaf

        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.

        1. Yoren Chang Avatar
          Yoren Chang

          Hey Michel, I’ll take a look at this over the weekend. Can you confirm it’s still work for 5.5 but 5.5.1 broke it?

          1. Michel van der Graaf Avatar
            Michel van der Graaf

            Hi Yoren, as far as I know, before the latest update 5.5.1 it worked fine.

          2. Yoren Chang Avatar
            Yoren Chang

            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

          3. Michel van der Graaf Avatar
            Michel van der Graaf

            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!

  16. 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!

    1. Yoren Chang Avatar
      Yoren Chang

      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!

      1. Yoren Chang Avatar
        Yoren Chang

        I have updated it to 0.8.1 but not sure if that can help tho. Let me know how it goes. Thanks.

  17. […] fields, it’s nearly impossible to find an image. Media Search Enhanced is a new plugin created by Yoren Chang that enables all fields for a media item to be searchable. With the plugin activated you […]

  18. Media Search Enhanced is really good plugin as stated in the article. We have gone through such issues with our clients.

  19. Frederic Avatar
    Frederic

    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

Leave a Reply

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