MSE Now Supporting MIME Type And Date Filters

The latest update for my Media Search Enhanced (MSE) plugin will be supporting MIME type and date filters in the Media Library. It’s funny that I never use these filters myself, so in the past two years there was such a major bug in the plugin and I never noticed. Read More…

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!

Inserting Media Search Form Into Posts Or Templates

I’ve got lots of plugin ideas but don’t have much time on developing recently, due to I’m fascinated by AngularJS since last November and prepare to grow my freelancing business. Despite that I still try to take some time to update my plugins based on suggestions from users.

Today I just 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 website visitors can search for media by all fields (title, caption, alt text, filename and terms).

I found it interesting because I’ve considered to implement functions like that but not sure if it’s of use enough. According to Don’s comment, it will be a very good idea.

So I made a major change that moved all main functions from admin class to public class in my plugin, and create a shortcode mse-search-form to insert a media search form into posts or templates. The media search form is a default WordPress search form but customized:

  1. Add a hidden field to force it only to search for attachment post type.
  2. Add a class name mse-search-form to help if user would like to modify the form with CSS.
  3. Two custom filters in the form: mse_search_form_class to change the form class name, mse_search_form_placeholder to change the input placeholder.

For now the frontend media search form might not be that helpful that it just display all media as posts in search results page. You have to custom the page to make sense of it, so maybe I should improve it in the next release by adding a media search results page or creating a media search results shortcode.

Other comments on the WP.org support forum proposed to display the media search results but group them by post id, and search for meta values. These opinions are useful to some extend so I’ll keep in mind. Your feedback is always welcome. Let’s improve the media search workflow together!

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!

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!