Skip to content

Instantly share code, notes, and snippets.

@eccentricpixel
Last active August 29, 2015 14:07
Show Gist options
  • Save eccentricpixel/c8ac4a7534facb51aa4c to your computer and use it in GitHub Desktop.
Save eccentricpixel/c8ac4a7534facb51aa4c to your computer and use it in GitHub Desktop.
Upgrade of Wordpress' media search function to find matches in the filename which is stored in the GUID column. In the end, a much more user-friendly (client-friendly) experience. Works with modal select windows too (ex: add media buttons).
/* Upgrade the Media search to allow for searching the filename.
* =========================================================== */
add_filter( 'posts_search', 'guid_search_media', 10, 2 );
function guid_search_media( $search, $a_wp_query )
{
global $wpdb, $pagenow;
// Only Admin side && Only Media Library page
if ( !is_admin() && 'upload.php' != $pagenow )
return $search;
// Original search string:
$search = str_replace(
'AND ((',
'AND (((' . $wpdb->prefix . 'posts.guid LIKE \'%' . $a_wp_query->query_vars['s'] . '%\') OR ',
$search
);
return $search;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment